简体   繁体   English

围绕旋转物体旋转物体?

[英]Rotate an object around a rotating object?

I am developing a solar system animation in Unity3D. 我正在Unity3D中开发太阳系动画。 Planets rotates around sun. 行星绕太阳旋转。 But ı have an issue simulating satellites like Moon. 但是我在模拟卫星如月亮时遇到了一个问题。 Moon should be rotating around world normally and moon should be rotating around World. 月亮应该绕着地球正常旋转,月亮应该绕着世界旋转。 Since World is rotating around Sun , i am having trouble about calculating true rotation for moon. 由于世界围绕太阳旋转,所以我很难计算月亮的真实旋转。 I don't want to use rotateAround() since it is deprecated. 我不想使用rotateAround(),因为它已被弃用。 I need to make this done using Rotate(). 我需要使用Rotate()完成此操作。

this is my planetScript 这是我的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 gameView 在此处输入图片说明

Moon is on the way !! 月亮在路上! I solved it using satellite object in planets script instead of assigning planet script to them. 我使用行星脚本中的卫星对象而不是为其分配行星脚本来解决该问题。 PlanetScript has an satellite GameObject now. PlanetScript现在有一个卫星GameObject。

  1. Check if the planet has satellite. 检查行星是否有卫星。
  2. if so rotate satellite as much as rotating the planet around Sun. 如果这样的话,旋转卫星就如同围绕太阳旋转行星一样。
  3. after rotating the planet and the satellite around the Sun now can be able to rotate the Satellite around the planet(For example World rotated 1 degree around Sun rotate satellite 1 degree around sun and rotate the moon x degree around The Planet) When World rotates 1 degree around Sun, Moon rotates approximately 12 degree around the World.(1 moon year = 30 days almost). 在使行星和卫星绕太阳旋转之后,现在可以使卫星绕行星旋转(例如,世界围绕太阳旋转1度,使卫星围绕太阳旋转1度,而月亮绕行星旋转x度)当世界旋转1围绕太阳转1度,月亮绕世界旋转大约12度。(每年1个月=大约30天)。 New Script 新剧本

    public class planetScript: MonoBehaviour { 公共类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; } } 

    } }

    Since every plane may not have a satellite, the script should be changed. 由于每个飞机可能没有卫星,因此应更改脚本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM