Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How to get an object to orbit a moving object without falling out of orbit

Discussion in 'Physics' started by StephenRJudge27, Dec 31, 2018.

  1. StephenRJudge27

    StephenRJudge27

    Joined:
    Sep 2, 2015
    Posts:
    24
    Hello Unity friends, I am trying to figure out how to get an object to orbit another object without falling out of the general orbit radius. I want to use rigidbody physics and I don't want to increase the velocity in order to have it keep up because it will orbit too fast. Does anyone have an idea how this could be accomplished?

    This is the code I am using to orbit the object. It works fine on stationary objects but once I add torque to the parent object things start to break down.

    Quaternion q = Quaternion.AngleAxis (stats.orbitSpeed * Time.deltaTime, transform.forward);
    rb.MovePosition (q * (rb.transform.position - target.position) + target.position);

    Here is a video of what is going on.



    This is how I want it to orbit around the moving object. In this video the parent is stationary.


    Thank you, and also Happy New Years everyone! :)
     
  2. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,584
    If you want orbiting object, simply make gravity, always pointing at planet from object, and then jus apply one of velocity, or just apply force. Rest physics will do Jo for you.
     
    StephenRJudge27 likes this.
  3. StephenRJudge27

    StephenRJudge27

    Joined:
    Sep 2, 2015
    Posts:
    24
    That would work however, for the purposes of my game using real gravity ended up causing a lot of problems so I have had to use faux solutions to get it to work. I actually just went back to not using any rigid body movement and I was able to get the result I was looking for. Thank you for your reply though!
     
  4. tylerdurdan

    tylerdurdan

    Joined:
    Feb 8, 2016
    Posts:
    3
     
  5. tylerdurdan

    tylerdurdan

    Joined:
    Feb 8, 2016
    Posts:
    3
    How do I make gravity always pointing at planet from object? Thanks
     
  6. tylerdurdan

    tylerdurdan

    Joined:
    Feb 8, 2016
    Posts:
    3
    what did you use instead of the Rigidbody?
     
  7. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,584
    Just calculate vector direction, between object and planet. Then apply gravity force.
     
  8. datagreed

    datagreed

    Joined:
    Sep 17, 2018
    Posts:
    42
    I tried this and obviously ended up with objects just falling on the surface of the planet instead of orbiting it
     
  9. AWildCoconut

    AWildCoconut

    Joined:
    Aug 8, 2020
    Posts:
    6
    If you are using an orbit script such as Brackeys one, you will need to have a velocity high enough for it to swing around the parent