[英]Rotate an object around a rotating object?
我正在Unity3D中開發太陽系動畫。 行星繞太陽旋轉。 但是我在模擬衛星如月亮時遇到了一個問題。 月亮應該繞着地球正常旋轉,月亮應該繞着世界旋轉。 由於世界圍繞太陽旋轉,所以我很難計算月亮的真實旋轉。 我不想使用rotateAround(),因為它已被棄用。 我需要使用Rotate()完成此操作。
這是我的planetScript
public class planetScript : MonoBehaviour {
public GameObject target;//target is Sun for World, World for Moon etc
public float rotateRatioCenter;//1 degree for World
private float rotateSpeedTarget;
public float rotateRatioAround;//365 for World
private float rotateSpeedAround;
public float counter = 0;
void Start () {
}
// Update is called once per frame
void Update () {
rotateSpeedTarget = rotateRatioCenter * gameMasterScript.rotateAroundCenterRatio;// rotateRatioCenter * 1 ,
rotateSpeedAround = rotateSpeedTarget * rotateRatioAround;
float yRotate = transform.eulerAngles.y;
transform.Rotate(Vector3.up, rotateSpeedAround);
rotateAroundTarget();
}
void rotateAroundTarget()//this is the method should be optimized
{
Quaternion quaRot = Quaternion.Euler(0, rotateSpeedTarget, 0);
transform.position = quaRot * (transform.position - target.transform.position) + target.transform.position;
}
}
月亮在路上! 我使用行星腳本中的衛星對象而不是為其分配行星腳本來解決該問題。 PlanetScript現在有一個衛星GameObject。
在使行星和衛星繞太陽旋轉之后,現在可以使衛星繞行星旋轉(例如,世界圍繞太陽旋轉1度,使衛星圍繞太陽旋轉1度,而月亮繞行星旋轉x度)當世界旋轉1圍繞太陽轉1度,月亮繞世界旋轉大約12度。(每年1個月=大約30天)。 新劇本
公共類planetScript:MonoBehaviour {
public GameObject target;//target is Sun for World, World for Moon etc public GameObject satellite; public float rotateRatioCenter;//1 degree for World public float rotateRatioAround;//365 for World private float rotateSpeedTarget; private float rotateSpeedAround; public float satelliterotateRatioCenter;//12x for Moon public float satelliteRotateRatioAround;//1 for Moon private float satelliteRotateSpeedTarget; private float satelliteRotateSpeedAround; public float counter = 0; void Start () { } // Update is called once per frame void Update () { rotateSpeedTarget = rotateRatioCenter * gameMasterScript.rotateAroundCenterRatio;// rotateRatioCenter * 1 , rotateSpeedAround = rotateSpeedTarget * rotateRatioAround; satelliteRotateSpeedTarget = satelliterotateRatioCenter * gameMasterScript.rotateAroundCenterRatio; satelliteRotateSpeedAround = satelliteRotateSpeedTarget * satelliteRotateRatioAround; transform.Rotate(Vector3.up, rotateSpeedAround); rotateAroundTarget(); } void rotateAroundTarget()//this is the method should be optimized { Quaternion quaRot = Quaternion.Euler(0, rotateSpeedTarget, 0); transform.position = quaRot * (transform.position - target.transform.position) + target.transform.position; if (satellite != null) { satellite.transform.Rotate(Vector3.up, satelliteRotateSpeedAround); satellite.transform.position = quaRot * (satellite.transform.position - target.transform.position) + target.transform.position; Quaternion quaRotSat = Quaternion.Euler(0, satelliteRotateSpeedTarget, 0); satellite.transform.position = quaRotSat * (satellite.transform.position - transform.position) + transform.position; } }
}
由於每個飛機可能沒有衛星,因此應更改腳本。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.