首先是制做太阳系中的每一个行星,基本上都是先建立Sophere,而后改变起始位置,添加材质和贴图,这里就不赘述了。
给每一个行星建立材质包:git
以后就是建立一个行星的移动脚本使得行星绕太阳公转起来,这里须要注意的就是随机选取或者本身设一个参照轴,使得每颗行星公转的法平面不一样。github
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Move : MonoBehaviour { public Transform origin; public float speed; float ry, rx; // Use this for initialization void Start() { speed = Random.Range(9, 12); rx = Random.Range(10, 60); ry = Random.Range(10, 60); } // Update is called once per frame void Update() { this.transform.RotateAround(origin.position, new Vector3(0, rx, ry), speed * Time.deltaTime); } }
将脚本挂载到全部行星上后全部行星就能动起来了。可是行星还不能自转,因而添加一个自转脚本挂载到全部星球上:dom
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Rotation : MonoBehaviour { // Use this for initialization void Start () { } // Update is called once per frame void Update () { this.transform.RotateAround(this.transform.position, Vector3.up, Random.Range(1, 3)); } }
这时全部的行星的移动就已经搞定了,须要注意的就是月亮绕地球的旋转须要一个单独的脚本,设定以地球为旋转圆心:this
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Moon_Move : MonoBehaviour { public Transform origin; public float speed = 4; float ry, rx; // Use this for initialization void Start() { rx = Random.Range(10, 60);//随机选取旋转轴向量 ry = Random.Range(10, 60); } // Update is called once per frame void Update() { this.transform.RotateAround(origin.position, new Vector3(0, rx, ry), speed * Time.deltaTime); } }
最后发现太阳系太过孤单,太空怎么能少了星海做为背景?添加一个背景板,贴上星空的图片做为背景美化一下:spa