【Unity3D】实现太阳系
阅读原文时间:2023年07月10日阅读:1

实践要求:写一个程序,实现一个完整的太阳系,其他星球围绕太阳的转速必须不一样,并且不再一个法平面内。

法平面是指过空间曲线的切点,且与切线垂直的平面。要求不在一个法平面内,则在保证所有行星以及太阳在一条轴上时,另外两条轴的比例不相同即可。公转速度在RotateAround参数里面设置。如:

这个程序在课堂程序的基础上完成,使用了预制、动态生成对象,在位置上使用Vector3定好行星初始位置,使用RotateAround设置行星公转,使用Rotate设置行星自转。参数大部分参照了太阳系的参数,如行星大小,公转速度等按比例模拟。

显示效果:


制作概述:

1.制作太阳系预制

根据太阳系各行星的大小设置Transform中的Scale

想要给白色的球贴上图的,可以去网上找到太阳系贴图,直接百度搜索就好。

然后导入图片资源,再将对应的图片拖到对应的行星即可。

最后将Sun整个拖入Assets/Resources/Perfabs(这一步是为了后续改进,直接放在Hierarchy里面后面再挂载cs文件就可以直接运行)

到这预制就做好啦~

2.开始编写代码~

使用了课程中的MVC架构

新建一个RoundSun.cs

cs文件:

2.1声明对象

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;  
public Transform Pluto;

2.2初始行星位置

void Start () {
Sun.position = Vector3.zero;
Mercury.position = new Vector3 (, , );
Venus.position = new Vector3 (, , );
Earth.position = new Vector3 (, , );
Moon.position = new Vector3 (, , );
Mars.position = new Vector3 (, , );
Jupiter.position = new Vector3 (, , );
Saturn.position = new Vector3 (, , );
Uranus.position = new Vector3 (, , );
Neptune.position = new Vector3 (, , );
Pluto.position = new Vector3 (, , );
}

2.3设置行星公转和自转

手动设置的参数,与真实太阳系有偏差

void Update () {
Vector3 a1 = new Vector3 (, , );
Vector3 a2 = new Vector3 (, , );
Vector3 a3 = new Vector3 (, , );
Vector3 a4 = new Vector3 (, , );
Vector3 a5 = new Vector3 (, , );
Vector3 a6 = new Vector3 (, , );
Vector3 a7 = new Vector3 (, , );
Vector3 a8 = new Vector3 (, , );
Vector3 a9 = new Vector3 (, , );

    Mercury.RotateAround (Sun.position, a1, \*Time.deltaTime);  
    Mercury.Rotate (Vector3.up\*\*Time.deltaTime);

    Venus.RotateAround (Sun.position, a2, \*Time.deltaTime);  
    Venus.Rotate (Vector3.up\*\*Time.deltaTime);

    Earth.RotateAround (Sun.position, a3, \*Time.deltaTime);  
    Earth.Rotate (Vector3.up\*\*Time.deltaTime);  
    Moon.transform.RotateAround (Earth.position, Vector3.up,  \* Time.deltaTime);

    Mars.RotateAround (Sun.position, a4, \*Time.deltaTime);  
    Mars.Rotate (Vector3.up\*\*Time.deltaTime);

    Jupiter.RotateAround (Sun.position, a5, \*Time.deltaTime);  
    Jupiter.Rotate (Vector3.up\*\*Time.deltaTime);

    Saturn.RotateAround (Sun.position, a6, \*Time.deltaTime);  
    Saturn.Rotate (Vector3.up\*\*Time.deltaTime);

    Uranus.RotateAround (Sun.position, a7, \*Time.deltaTime);  
    Uranus.Rotate (Vector3.up\*\*Time.deltaTime);

    Neptune.RotateAround (Sun.position, a8, \*Time.deltaTime);  
    Neptune.Rotate (Vector3.up\*\*Time.deltaTime);

    Pluto.RotateAround (Sun.position, a9, \*Time.deltaTime);  
    Pluto.Rotate (Vector3.up\*\*Time.deltaTime);  
}

这时候直接将cs挂载到Sun里,到这里已经可以运行实现啦。


接下来实现预制,动态生成对象吧。直接放代码。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FirstController : MonoBehaviour, ISceneController {
void Awake() {
Debug.Log ("load sunt…\n");
SSDirector director = SSDirector.getInstance ();
director.setFPS ();
director.currentSceneController = this;
director.currentSceneController.LoadResources ();
}

public void LoadResources() {  
    GameObject sunset = Instantiate<GameObject> (  
                            Resources.Load <GameObject> ("Perfabs/Sun"),  
                            Vector3.zero, Quaternion.identity);  
    sunset.name = "sunset";  
    Debug.Log ("load sunset...\\n");  
}  
public void Pause(){  
}  
public void Resume(){  
}  
// Use this for initialization  
void Start () {

}

// Update is called once per frame  
void Update () {

}  

}

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public interface ISceneController {  
    void LoadResources();  
    void Pause();  
    void Resume();  
}

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SSDirector : System.Object {
private static SSDirector _instance;

public ISceneController currentSceneController { get; set; }  
public bool running{ get; set; }

public static SSDirector getInstance() {  
    if (\_instance == null) {  
        \_instance = new SSDirector ();  
    }  
    return \_instance;  
}

public int getFPS() {  
    return Application.targetFrameRate;  
}

public void setFPS(int fps) {  
    Application.targetFrameRate = fps;  
}  

}

在确保

在这个文件目录下

将FirstController挂载到主摄像机或者空对象即可运行

大致架构:

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章