[英]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.