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

相关文章
相关标签/搜索