實踐要求:寫一個程序,實現一個完整的太陽系,其餘星球圍繞太陽的轉速必須不同,而且再也不一個法平面內。架構
法平面是指過空間曲線的切點,且與切線垂直的平面。要求不在一個法平面內,則在保證全部行星以及太陽在一條軸上時,另外兩條軸的比例不相同便可。公轉速度在RotateAround參數裏面設置。如:ide
這個程序在課堂程序的基礎上完成,使用了預製、動態生成對象,在位置上使用Vector3定好行星初始位置,使用RotateAround設置行星公轉,使用Rotate設置行星自轉。參數大部分參照了太陽系的參數,如行星大小,公轉速度等按比例模擬。this
顯示效果:spa
製做概述:code
1.製做太陽系預製orm
根據太陽系各行星的大小設置Transform中的Scale對象
想要給白色的球貼上圖的,能夠去網上找到太陽系貼圖,直接百度搜索就好。blog
而後導入圖片資源,再將對應的圖片拖到對應的行星便可。圖片
最後將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掛載到主攝像機或者空對象便可運行
大體架構: