围绕一个旋转的物体旋转一个物体?
Rotate an object around a rotating object?
我正在Unity3D中开发太阳系动画。行星围绕太阳旋转。但是我在模拟像月球这样的卫星时遇到了问题。月亮应该正常绕着世界旋转,月亮应该绕着世界旋转。由于 World 围绕 Sun 旋转,我在计算月球的真实旋转时遇到了麻烦。我不想使用 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;
}
}
gameView
月亮来了!!
我使用行星脚本中的卫星对象解决了它,而不是将行星脚本分配给它们。 PlanetScript 现在有一个卫星游戏对象。
- 检查行星是否有卫星。
- 如果这样的话,卫星旋转的速度与行星绕太阳旋转的速度一样多。
行星和卫星绕太阳旋转后现在可以使卫星绕行星旋转(例如World rotated 1 degree around Sun rotate satellite 1 degree around sun and rotate the moon围绕地球 x 度)
当世界围绕太阳旋转 1 度时,月亮围绕世界旋转大约 12 度。(1 月亮年 = 30 天差不多)。
新脚本
public class 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;
}
}
}
因为不一定每架飞机都有卫星,所以脚本应该改一下。
我正在Unity3D中开发太阳系动画。行星围绕太阳旋转。但是我在模拟像月球这样的卫星时遇到了问题。月亮应该正常绕着世界旋转,月亮应该绕着世界旋转。由于 World 围绕 Sun 旋转,我在计算月球的真实旋转时遇到了麻烦。我不想使用 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;
}
}
gameView
月亮来了!! 我使用行星脚本中的卫星对象解决了它,而不是将行星脚本分配给它们。 PlanetScript 现在有一个卫星游戏对象。
- 检查行星是否有卫星。
- 如果这样的话,卫星旋转的速度与行星绕太阳旋转的速度一样多。
行星和卫星绕太阳旋转后现在可以使卫星绕行星旋转(例如World rotated 1 degree around Sun rotate satellite 1 degree around sun and rotate the moon围绕地球 x 度) 当世界围绕太阳旋转 1 度时,月亮围绕世界旋转大约 12 度。(1 月亮年 = 30 天差不多)。 新脚本
public class 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; } }
}
因为不一定每架飞机都有卫星,所以脚本应该改一下。