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

Rotatearound change speed based on name or distance from pivot point

Discussion in 'Scripting' started by BMRX, Feb 18, 2016.

  1. BMRX

    BMRX

    Joined:
    Nov 23, 2014
    Posts:
    51
    Hey sorry if my title doesn't make sense I'm not sure how else to ask.

    I'm working on a 2D project that generates star systems. Right now my current issue is that I want the speed of the generated planets that are farther away from the centre to be slower than the ones that are closer.

    I was thinking that the way I could go about this is using the name that is added to the instantiated planets (Planet N° X) if X is larger than 1 it would decrease the speed based on how much larger of a number it is compared to the original planet (Planet N° 1).

    Though that seems fairly complicated, so my other idea is to decrease the speed based on how far away it is from the rotate point.
    Code (csharp):
    1. public class OrbitalController : MonoBehaviour {
    2.  
    3.     public float minOrbitSpeed;
    4.     public float maxOrbitSpeed;
    5.     public float orbitSpeed;
    6.     public float speedDecrease;
    7.     private float curSpeed;
    8.  
    9.     // Use this for initialization
    10.     void Start () {
    11.         orbitSpeed = Random.Range(minOrbitSpeed, maxOrbitSpeed);
    12.         curSpeed = orbitSpeed;
    13.         speedDecrease = curSpeed - 5.0f;
    14.      
    15.     }
    16.  
    17.     // Update is called once per frame
    18.     void Update () {
    19.         transform.RotateAround(transform.parent.position, Vector3.forward, orbitSpeed * Time.deltaTime);
    20.        // Debug.Log(this.name + " speed: " + curSpeed);
    21.     }
    22. }
    23.  
    Here is the script I use to handle planet orbit.

    What do you fine folks think? I'm having a massive brain fart about this. A poke in the right direction would be great.
     
  2. Hyblademin

    Hyblademin

    Joined:
    Oct 14, 2013
    Posts:
    725
    Looks like the parent object is the orbited body, right? Instead of setting minOrbitSpeed and maxOrbitSpeed in the inspector, set them in Start() by using some formula based on the distance to the parent object using Vector3.Distance().

    Making it an inverse square relationship would make sense to me:

    Code (CSharp):
    1. ...
    2.  
    3. public float minOrbitConstant;
    4. public float maxOrbitConstant;
    5.  
    6. void Start() {
    7.      float distToParent = Vector3.Distance(transform.position, transform.parent.position);
    8.  
    9.      minOrbitSpeed = minOrbitConstant / (distToParent * distToParent);
    10.      maxOrbitSpeed = maxOrbitConstant / (distToParent * distToParent);
    11.  
    12.      ...
    13.  
    14. }
    15.  
    16. ...
    You'd have to play with the constants to get the speeds where you want them, of course.

    , you could calculate a single orbit speed, and add some noise (random value) function to it.
     
    Last edited: Feb 18, 2016
    BMRX likes this.
  3. BMRX

    BMRX

    Joined:
    Nov 23, 2014
    Posts:
    51
    Yes the parent object is the orbited body.

    Vector3.Distance looks like the right direction, thank you very much Hyblademin!
     
  4. Hyblademin

    Hyblademin

    Joined:
    Oct 14, 2013
    Posts:
    725
    No problem. I accidentally posted my last reply before I was done, just wanted you to know that I edited it with an example of what I was going for.
     
    BMRX likes this.
  5. BMRX

    BMRX

    Joined:
    Nov 23, 2014
    Posts:
    51
    Thank you for your example, I would have gone that route except that with a randomized orbitspeed value in the script (which is attatched to every planet object) the speeds would be different regardless.

    So I went this route instead:
    Code (csharp):
    1. public float orbitSpeed;
    2.     public float curSpeed;
    3.     private float radius;
    4.  
    5.     // Use this for initialization
    6.     void Start () {
    7.         orbitSpeed = 9001f;
    8.      
    9.     }
    10.  
    11.     // Update is called once per frame
    12.     void Update () {
    13.         radius = Vector3.Distance(transform.position, transform.parent.position);
    14.         curSpeed = orbitSpeed / Mathf.Sqrt(radius);
    15.         transform.RotateAround(transform.parent.position, Vector3.forward, curSpeed * Time.deltaTime * 180 / (2 * Mathf.PI * radius));
    16.     }
    EDIT/ And just now I realized that there is no need for curSpeed and radius to be in Update().
     
  6. Hyblademin

    Hyblademin

    Joined:
    Oct 14, 2013
    Posts:
    725
    OK, great, glad you have a solution now.

    By the way, I wasn't suggesting that you randomize the orbit speed, but to perhaps have a centered speed that varies with the distance, and add a small random value to it so that you could get the range-of-speeds effect you seemed to be going for. Either way, it was just an alternate suggestion, and the code I wrote doesn't include randomizing.