简体   繁体   中英

Rotate an object around a rotating object?

I am developing a solar system animation in 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. I need to make this done using Rotate().

this is my 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 在此处输入图片说明

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.

  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). New Script

    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; } } 

    }

    Since every plane may not have a satellite, the script should be changed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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