Unity3d学习笔记(2)--一个太阳系的实现

    xiaoxiao2021-03-25  70

      这是实现一个简单的太阳系,包括太阳和八大行星和月亮。

    这个程序采用MVC结构

     

    总共有6个cs文件:

           SSDirector.cs:控制场景的切换,指明哪个是当前场景

           FristController.cs:控制第一个场景的资源加载和配置

           roundsun.cs:挂载在太阳上的组件,使其它行星绕太阳转,月亮绕地球转和各天体的自转

           UserGUI.cs

           IUserAction.cs

           ISceneController.cs

    因为时间问题,上面有些文件的功能还没有实现,但因为要满足MVC结构的问题,便于以后的扩展和修改,就把它们

    加进了项目中。

    FristController.cs

           这个文件主要是用来加载太阳系和以及为roundsun组件传递参数。

    public void LoadResources() { GameObject solar_system = Instantiate<GameObject>( Resources.Load<GameObject>("prefabs/sun_shadow"), Vector3.zero, Quaternion.identity); GameObject sun = Instantiate<GameObject>( Resources.Load<GameObject>("prefabs/sun"), Vector3.zero, Quaternion.identity); solar_system.name = "sun_shadow"; sun.name = "sun"; GameObject[] body = new GameObject[body_num]; for (int i = 0; i < body_num; i++) { body[i] = GameObject.Find(body_name[i]); } Debug.Log("load solar_system ...\n"); roundsun com_sun = solar_system.GetComponent("roundsun") as roundsun; com_sun.sun_shadow = body[0].transform; com_sun.mercury = body[1].transform; com_sun.venus = body[2].transform; com_sun.earth_shadow = body[3].transform; com_sun.moon = body[4].transform; com_sun.mars = body[5].transform; com_sun.jupiter = body[6].transform; com_sun.saturn = body[7].transform; com_sun.uranus = body[8].transform; com_sun.neptune = body[9].transform; com_sun.sun = body[10].transform; com_sun.earth = body[11].transform; }

    roundsun.cs

     

      设置各天体的初始位置:

    void Start () { sun.position = Vector3.zero; sun_shadow.position = sun.position; mercury.position = new Vector3(0.7f, 0, 0); venus.position = new Vector3(1.2f, 0, 0); earth.position = new Vector3(2.2f, 0, 0); earth_shadow.position = earth.position; moon.position = new Vector3(2.7f, 0, 0); mars.position = new Vector3(3.3f, 0, 0); jupiter.position = new Vector3(4.5f, 0, 0); saturn.position = new Vector3(6.0f, 0, 0); uranus.position = new Vector3(7.0f, 0, 0); neptune.position = new Vector3(8.0f, 0, 0); } 设置各天体的公转和自转:

    void Update () { mercury.RotateAround(sun_shadow.position, new Vector3(1, 7f, 0), 240 * Time.deltaTime); venus.RotateAround(sun_shadow.position, new Vector3(1, 12f, 0), 70 * Time.deltaTime); earth.RotateAround(sun_shadow.position, Vector3.up, 60* Time.deltaTime); earth_shadow.position = earth.position; mars.RotateAround(sun_shadow.position, new Vector3(1, 16f,0), 30 * Time.deltaTime); jupiter.RotateAround(sun_shadow.position, new Vector3(1, 18f, 0), 20 * Time.deltaTime); saturn.RotateAround(sun_shadow.position, new Vector3(1, 15f, 0), 15 * Time.deltaTime); uranus.RotateAround(sun_shadow.position, new Vector3(1, 20f, 0), 10 * Time.deltaTime); neptune.RotateAround(sun_shadow.position, new Vector3(1, 17f, 0), 5 * Time.deltaTime); moon.transform.RotateAround(earth_shadow.position, Vector3.up, 359 * Time.deltaTime); sun.Rotate(Vector3.up * 10 * Time.deltaTime); mercury.Rotate(Vector3.up * 10 * Time.deltaTime); venus.Rotate(Vector3.down * 5 * Time.deltaTime); earth.Rotate(Vector3.up * 30 * Time.deltaTime); mars.Rotate(Vector3.up * 30 * Time.deltaTime); jupiter.Rotate(Vector3.up * 65 * Time.deltaTime); saturn.Rotate(Vector3.up * 67 * Time.deltaTime); uranus.Rotate(Vector3.up * 50 * Time.deltaTime); neptune.Rotate(Vector3.up * 45 * Time.deltaTime); moon.Rotate(Vector3.up * 15 * Time.deltaTime); } 写博客时发现天体的自转是不是应该挂在各天体上比较符合

    转载请注明原文地址: https://ju.6miu.com/read-38330.html

    最新回复(0)