Search Unity

How to calculate the object position in space with altitude?

Discussion in 'Scripting' started by Albertkaruna, Mar 2, 2018.

  1. Albertkaruna

    Albertkaruna

    Joined:
    Apr 12, 2015
    Posts:
    9
    I have some flight locations, i would like to place a unity objects based on those flight locations. I already calculated spherical location with latitude and longitude without using altitude like this,


    for (int i = 0; i < 1000; i++) {
    lati = latitude ;
    longi = longitude ;
    alti = altitude ;

    lati = Mathf.PI * lati / 180;
    longi = Mathf.PI * longi / 180;
    alti = Mathf.PI * alti / 180;

    // adjust position by radians
    lati -= 1.570795765134f; // subtract 90 degrees (in radians)

    // and switch z and y (since z is forward)
    float xPos = (radius) * Mathf.Sin (lati) * Mathf.Cos (longi);
    float zPos = (radius) * Mathf.Sin (lati) * Mathf.Sin (longi);
    float yPos = (radius) * Mathf.Cos (lati);

    GameObject flight = Instantiate (aircraft.gameObject, new Vector3 (xPos, yPos, zPos), Quaternion.identity);
    }

    It gives something like in the image, 1111.png 1111.png


    Now i need to use the altitude to create the real life like flight position.
    How can i achieve this?

    Thanks you.
     
    Last edited: Mar 3, 2018
  2. jschieck

    jschieck

    Joined:
    Nov 10, 2010
    Posts:
    429
    If you want different altitudes, just add radius + altitude, that'll put your flight objects further out from your "earth" center
     
  3. Albertkaruna

    Albertkaruna

    Joined:
    Apr 12, 2015
    Posts:
    9

    Thanks for the response, I think it's better now but that straight line is something weird. I added radius + altitude to all axis.
     

    Attached Files: