【Unity3D】實現太陽系


 

    實踐要求:寫一個程序,實現一個完整的太陽系,其他星球圍繞太陽的轉速必須不一樣,並且不再一個法平面內。

    法平面是指過空間曲線的切點,且與切線垂直的平面。要求不在一個法平面內,則在保證所有行星以及太陽在一條軸上時,另外兩條軸的比例不相同即可。公轉速度在RotateAround參數里面設置。如:

    這個程序在課堂程序的基礎上完成,使用了預制、動態生成對象,在位置上使用Vector3定好行星初始位置,使用RotateAround設置行星公轉,使用Rotate設置行星自轉。參數大部分參照了太陽系的參數,如行星大小,公轉速度等按比例模擬。

顯示效果:


 

制作概述:

1.制作太陽系預制

根據太陽系各行星的大小設置Transform中的Scale

想要給白色的球貼上圖的,可以去網上找到太陽系貼圖,直接百度搜索就好。

然后導入圖片資源,再將對應的圖片拖到對應的行星即可。

最后將Sun整個拖入Assets/Resources/Perfabs(這一步是為了后續改進,直接放在Hierarchy里面后面再掛載cs文件就可以直接運行)

到這預制就做好啦~

 

2.開始編寫代碼~

使用了課程中的MVC架構

新建一個RoundSun.cs

cs文件:

 

2.1聲明對象

    public Transform Sun;
    public Transform Mercury;
    public Transform Venus;
    public Transform Earth;
        public Transform Moon;
    public Transform Mars;
    public Transform Jupiter;
    public Transform Saturn;
    public Transform Uranus;
    public Transform Neptune;
    public Transform Pluto;

2.2初始行星位置

void Start () {
        Sun.position = Vector3.zero;
        Mercury.position = new Vector3 (4, 0, 0);
        Venus.position = new Vector3 (6, 0, 0);
        Earth.position = new Vector3 (8, 0, 0);
            Moon.position = new Vector3 (10, 0, 0);
        Mars.position = new Vector3 (12, 0, 0);
        Jupiter.position = new Vector3 (16, 0, 0);
        Saturn.position = new Vector3 (20, 0, 0);
        Uranus.position = new Vector3 (24, 0, 0);
        Neptune.position = new Vector3 (28, 0, 0);
        Pluto.position = new Vector3 (32, 0, 0);
    }

2.3設置行星公轉和自轉

手動設置的參數,與真實太陽系有偏差

void Update () {
        Vector3 a1 = new Vector3 (0, 9, 2);
        Vector3 a2 = new Vector3 (0, 257, 135);
        Vector3 a3 = new Vector3 (0, 45, 339);
        Vector3 a4 = new Vector3 (0, 4, 9);
        Vector3 a5 = new Vector3 (0, 8, 19);
        Vector3 a6 = new Vector3 (0, 11, 9);
        Vector3 a7 = new Vector3 (0, 6, 137);
        Vector3 a8 = new Vector3 (0, 3, 13);
        Vector3 a9 = new Vector3 (0, 13, 122);
        
        Mercury.RotateAround (Sun.position, a1, 20*Time.deltaTime);
        Mercury.Rotate (Vector3.up*50*Time.deltaTime);

        Venus.RotateAround (Sun.position, a2, 10*Time.deltaTime);
        Venus.Rotate (Vector3.up*30*Time.deltaTime);

        Earth.RotateAround (Sun.position, a3, 10*Time.deltaTime);
        Earth.Rotate (Vector3.up*30*Time.deltaTime);
        Moon.transform.RotateAround (Earth.position, Vector3.up, 359 * Time.deltaTime);

        Mars.RotateAround (Sun.position, a4, 8*Time.deltaTime);
        Mars.Rotate (Vector3.up*30*Time.deltaTime);

        Jupiter.RotateAround (Sun.position, a5, 7*Time.deltaTime);
        Jupiter.Rotate (Vector3.up*30*Time.deltaTime);

        Saturn.RotateAround (Sun.position, a6, 6*Time.deltaTime);
        Saturn.Rotate (Vector3.up*30*Time.deltaTime);

        Uranus.RotateAround (Sun.position, a7, 5*Time.deltaTime);
        Uranus.Rotate (Vector3.up*30*Time.deltaTime);

        Neptune.RotateAround (Sun.position, a8, 4*Time.deltaTime);
        Neptune.Rotate (Vector3.up*30*Time.deltaTime);

        Pluto.RotateAround (Sun.position, a9, 3*Time.deltaTime);
        Pluto.Rotate (Vector3.up*30*Time.deltaTime);
    }

這時候直接將cs掛載到Sun里,到這里已經可以運行實現啦。


 

 

接下來實現預制,動態生成對象吧。直接放代碼。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FirstController : MonoBehaviour, ISceneController {
    void Awake() {
        Debug.Log ("load sunt...\n");
        SSDirector director = SSDirector.getInstance ();
        director.setFPS (60);
        director.currentSceneController = this;
        director.currentSceneController.LoadResources ();
    }

    public void LoadResources() {
        GameObject sunset = Instantiate<GameObject> (
                                Resources.Load <GameObject> ("Perfabs/Sun"),
                                Vector3.zero, Quaternion.identity);
        sunset.name = "sunset";
        Debug.Log ("load sunset...\n");
    }
    public void Pause(){
    }
    public void Resume(){
    }
    // Use this for initialization
    void Start () {
        
    }
    
    // Update is called once per frame
    void Update () {
        
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

    public interface ISceneController {
        void LoadResources();
        void Pause();
        void Resume();
    }
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SSDirector : System.Object {
    private static SSDirector _instance;

    public ISceneController currentSceneController { get; set; }
    public bool running{ get; set; }

    public static SSDirector getInstance() {
        if (_instance == null) {
            _instance = new SSDirector ();
        }
        return _instance;
    }

    public int getFPS() {
        return Application.targetFrameRate;
    }

    public void setFPS(int fps) {
        Application.targetFrameRate = fps;
    }
}

在確保

在這個文件目錄下

將FirstController掛載到主攝像機或者空對象即可運行

 大致架構:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM