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

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

这个程序采用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);
}
写博客时发现天体的自转是不是应该挂在各天体上比较符合


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值