Search Unity

Orbit while facing 90 degrees - Efficiency

Discussion in '2D' started by directusgames, Jan 6, 2017.

  1. directusgames

    directusgames

    Joined:
    May 8, 2015
    Posts:
    14
    Hi guys,

    I'm trying to orbit ships around a planet while keeping them facing at 90 degrees. I have this working for the most part but find as the number of ships increases, I'm losing FPS due to the rotation method.

    upload_2017-1-6_16-33-58.png

    Each ship is moving forward a constant velocity. The rotation method I'm currently using is:

    Code (CSharp):
    1.     public void Rotate90degs()
    2.     {
    3.         Vector3 diff = planet.transform.position - transform.position;
    4.         float rot_z = (Mathf.Atan2(diff.y, diff.x) * Mathf.Rad2Deg) /* -90 */;
    5.         Quaternion newRot = Quaternion.Euler(new Vector3(0,0,rot_z));
    6.         transform.rotation = Quaternion.RotateTowards(transform.rotation, newRot, Time.deltaTime * 75f);  
    7.     }
    8.  
    I know I can make them childs of a rotating game object, but that's not really going to suit my purposes for this.

    Any other suggestions on a more efficient way to do this?
     
  2. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    899
    It might be faster to do this:
    Code (CSharp):
    1.     public void Rotate90degs()
    2.     {
    3.         Vector3 diff = planet.transform.position - transform.position;
    4.         transform.right = diff;
    5.     }
    Which transform.* you need to use will depend on the direction your ships have from the start. There is; transform.up and transform.right available and to get down and left you just use those available and change the direction of your diff-vector, that is you use -diff.
     
    directusgames likes this.
  3. directusgames

    directusgames

    Joined:
    May 8, 2015
    Posts:
    14
    Thanks PGJ, that definitely helped!