1. 程式人生 > >Unity設定物體的自轉和公轉

Unity設定物體的自轉和公轉

正好要做一個天空的場景,想新增上行星和恆星的自轉和公轉,程式碼如下

1.自轉。

public float _RotationSpeed; //定義自轉的速度

transform.Rotate(Vector3.down*_RotationSpeed,Space.World); //物體自轉

2.公轉

    public GameObject Axis; //物體需要公轉的參照物
    public float _RotationSpeed; //公轉速度

 this.transform.RotateAround(Axis.transform.position, Vector3.up, _RotationSpeed);//將需要公轉的參照物拖入,設定公轉