開發(fā)環(huán)境:Unity 2022.3.5f1c1 + Visual Studio 2022
太陽系相關(guān)星體:太陽、八大行星、月球
模擬星系:太陽系、地月系
功能:支持行星以太陽為中心,任意軸進(jìn)行公轉(zhuǎn),此處演示同一平面。a1-a8為公轉(zhuǎn)軸,可以任意修改,InitStarPosition()將會修正星體初始位置;MoveAroundSun腳本掛在SolarSystemGroup對象上(空對象)。
動態(tài)演示效果
靜態(tài)展示圖片文章來源:http://www.zghlxwxcb.cn/news/detail-728128.html
核心代碼文章來源地址http://www.zghlxwxcb.cn/news/detail-728128.html
using UnityEngine;
public class MoveAroundSun : MonoBehaviour
{
// 星體
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;
// 公轉(zhuǎn)軸:Mercury至Neptune八大行星
Vector3 a1 = new Vector3(0, 1, 0);
Vector3 a2 = new Vector3(0, 1, 0);
Vector3 a3 = new Vector3(0, 1, 0);
Vector3 a4 = new Vector3(0, 1, 0);
Vector3 a5 = new Vector3(0, 1, 0);
Vector3 a6 = new Vector3(0, 1, 0);
Vector3 a7 = new Vector3(0, 1, 0);
Vector3 a8 = new Vector3(0, 1, 0);
/// <summary>
/// 初始化行星位置,使Star位于以Center為中心,rotateAxis為公轉(zhuǎn)軸的法平面上
/// </summary>
/// <param name="center">公轉(zhuǎn)中心</param>
/// <param name="star">公轉(zhuǎn)行星</param>
/// <param name="rotateAxis">公轉(zhuǎn)軸</param>
void InitStarPosition(Vector3 center, Transform star, Vector3 rotateAxis)
{
// 中心點指向行星的方向向量
Vector3 centerToStar = star.position - center;
// 通過方向向量和公轉(zhuǎn)軸向量的叉乘,得到這兩個向量所在平面的法向量
Vector3 normal = Vector3.Cross(rotateAxis, centerToStar);
// 計算方向向量和公轉(zhuǎn)軸向量的角度
float angle = Vector3.Angle(rotateAxis, centerToStar);
// 計算方向向量轉(zhuǎn)動到法向量的角度(轉(zhuǎn)動到法平面上的角度)
float rotateAngle = Mathf.Abs(90 - angle);
// 以太陽為中心,法向量為轉(zhuǎn)軸來轉(zhuǎn)動角度
star.RotateAround(center, normal, rotateAngle);
}
// Start is called before the first frame update
void Start()
{
// 為了讓任意位置的星體都可以繞任意軸旋轉(zhuǎn),需要在初始時刻定位星體位置(只掛載一個腳本,所以有重復(fù)代碼)
InitStarPosition(Sun.position, Mercury, a1);
InitStarPosition(Sun.position, Venus, a2);
InitStarPosition(Sun.position, Earth, a3);
InitStarPosition(Sun.position, Mars, a4);
InitStarPosition(Sun.position, Jupiter, a5);
InitStarPosition(Sun.position, Saturn, a6);
InitStarPosition(Sun.position, Uranus, a7);
InitStarPosition(Sun.position, Neptune, a8);
InitStarPosition(Earth.position, Moon, Earth.transform.up);
}
// Update is called once per frame
void Update()
{
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);
}
}
到了這里,關(guān)于Unity實現(xiàn)簡易太陽系的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!