• Unity实现简易太阳系


    开发环境Unity 2022.3.5f1c1 + Visual Studio 2022
    太阳系相关星体:太阳、八大行星、月球
    模拟星系太阳系、地月系
    功能:支持行星以太阳为中心,任意轴进行公转,此处演示同一平面。
    a1-a8为公转轴,可以任意修改,InitStarPosition()将会修正星体初始位置;MoveAroundSun脚本挂在SolarSystemGroup对象上(空对象)。
    动态演示效果

    静态展示图片
    在这里插入图片描述

    核心代码

    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;
    
        // 公转轴: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);
    
        /// 
        /// 初始化行星位置,使Star位于以Center为中心,rotateAxis为公转轴的法平面上
        /// 
        /// 公转中心
        /// 公转行星
        /// 公转轴
        void InitStarPosition(Vector3 center, Transform star, Vector3 rotateAxis)
        {
            // 中心点指向行星的方向向量
            Vector3 centerToStar = star.position - center;
            // 通过方向向量和公转轴向量的叉乘,得到这两个向量所在平面的法向量
            Vector3 normal = Vector3.Cross(rotateAxis, centerToStar);
            // 计算方向向量和公转轴向量的角度
            float angle = Vector3.Angle(rotateAxis, centerToStar);
            // 计算方向向量转动到法向量的角度(转动到法平面上的角度)
            float rotateAngle = Mathf.Abs(90 - angle);
            // 以太阳为中心,法向量为转轴来转动角度
            star.RotateAround(center, normal, rotateAngle);
        }
    
        // Start is called before the first frame update
        void Start()
        {
            // 为了让任意位置的星体都可以绕任意轴旋转,需要在初始时刻定位星体位置(只挂载一个脚本,所以有重复代码)
            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);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
  • 相关阅读:
    Linux性能模拟测试
    Java的数组使用
    快排&超详细,Leetcode排序数组题目带你升华掌握
    Python之第六章 内置容器 --- 字符串
    ARM cortex-A7核UART实验 收发数据
    Ubuntu 20.04上docker安装RabbitMQ并确保可以访问RabbitMQ的管理界面
    算法-复杂度
    Linux系列之:9秒钟掌握多种实时查询日志的方法,再也不用cat命令了。
    模拟实现库函数,strtsr,memmove.
    790.多米诺和托米诺平铺(DP)
  • 原文地址:https://blog.csdn.net/qq_40945965/article/details/132998964