Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Flying around and between planets

Discussion in 'Scripting' started by echoic, Jan 16, 2015.

  1. echoic

    echoic

    Joined:
    Apr 29, 2011
    Posts:
    89
    Hi peeps,

    I'm trying to make my character fly around an object, like in this game:

    http://www.y8.com/games/gravity_gambit

    Right now I have a semi-working script that involves planets with rotating transforms, and making the game character a child of one if it gets close enough. Trouble is, when I try to launch away from the planet, I go in the wrong direction.

    Any idea how I might achieve this? I don't think using real physics is a good idea, since this project is 2D.

    Thanks in advance!
     
  2. Polymorphik

    Polymorphik

    Joined:
    Jul 25, 2014
    Posts:
    599
    Interesting...yeah using F = Gm_1 * m_2 / r^2 and F = ma won't work here, your best bet is to check when you are launching if the player is parented if not unparent and apply a force in the transform.forward position. This should propel it forward if the rotations are set properly. Then OnTriggerEnter2D you can have it parent to the current planet and have it orbit the given planet. Orbiting since you are just using a 2D environment you can use the elliptical equations for an ellipse and use that to "orbit" the object given a distance and a speed.
     
  3. echoic

    echoic

    Joined:
    Apr 29, 2011
    Posts:
    89
    Hmmm... can't seem to get that to work either. Closest I've come is using translate instead of applying rigidbody forces.
     
  4. BenZed

    BenZed

    Joined:
    May 29, 2014
    Posts:
    524
    I wouldn't use parenting. I'd keep track of the target transform, and then write a custom function to twirl around it.

    This method, which I use frequently, would be perfect:

    Code (CSharp):
    1.        
    2. //Simply put 'this' in front of the first Vector2 argument to make it an Extension Method.
    3. public static Vector2 PointOnCircle(Vector2 origin, float radius, float angle) {
    4.  
    5.       float x = radius * Mathf.Cos (angle * Mathf.Deg2Rad) + origin.x;
    6.       float y = radius * Mathf.Sin (angle * Mathf.Deg2Rad) + origin.y;
    7.  
    8.       return new Vector2(x,y);
    9. }
    10.  
    I whipped up a quick, crude example, of which the pertinent code is in the ships script:
    Code (CSharp):
    1.     void FlyToAndAroundCurrentPlanet() {
    2.  
    3.         float heightFromSurface = currentPlanet.diameter * 0.5f + orbitHeight;
    4.  
    5.         //Pick a spot on an imaginary circle surrounding the planet, on orbitPhase angle around that circle
    6.         Vector3 targetPosition = PointOnCircle (currentPlanet.transform.position, heightFromSurface, orbitPhase);
    7.  
    8.         //Lerp to that spot
    9.         transform.position = Vector3.Lerp (transform.position, targetPosition, Time.deltaTime * 10f);
    10.  
    11.         //Increment the orbit phase to spin around it.
    12.         orbitPhase += Time.deltaTime * orbitSpeed;
    13.  
    14.  
    15.     }
    16.  
    Just import and run the ClickToOrbit scene. Click on any planet and your ship will fly to and around it.
     

    Attached Files:

    Polymorphik likes this.
  5. echoic

    echoic

    Joined:
    Apr 29, 2011
    Posts:
    89
    You're awesome doooood. Thanks!
     
  6. Polymorphik

    Polymorphik

    Joined:
    Jul 25, 2014
    Posts:
    599
    Officer Doofy has yet again solved a case :)