Unity——模擬太陽地球月球公轉自轉

1.建立3個球體並從新命名爲Sun、Earth、Moon,在建立球體時須要注意爲了讓地球和月球保持相對位置,須要把月球設爲地球的子物體,而後調整它們的大小以及位置,賦上貼紙。this

2.建立兩個腳本分別控制它們的自轉和公轉3d

腳本代碼以下:code

自轉:將自轉的腳本賦給太陽,地球和月亮orm

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

public class Rotation : MonoBehaviour 
{
	public float RotateSpeed=1f;
	// Use this for initialization
	void Start () 
        {	
	}
	// Update is called once per frame
	void Update () 
        {
	    this.transform.Rotate(Vector3.up*RotateSpeed);
	}
}

公轉:將公轉的腳本賦給地球和月亮blog

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

public class Revolution : MonoBehaviour 
{
	public Transform Target;
	public float AroundSpeed=1f;
	// Use this for initialization
	void Start () 
        {	
	}
	// Update is called once per frame
	void Update () 
        {
	    this.transform.RotateAround(Target.position,Vector3.up,AroundSpeed);
	}
}

3.將Earth拖動到Moon的Target中get

將Sun拖動到Earth的Targetit

相關文章
相關標籤/搜索