Search Unity

Follow on planet spherical

Discussion in 'Scripting' started by Bolt, Sep 25, 2017.

  1. Bolt

    Bolt

    Joined:
    Dec 15, 2012
    Posts:
    296
    I did not like fixing this problem. My game takes place on a spherical planet and the enemy must follow the player and then rotate in the direction of the target and according to the spherical shape of the planet
    Code (csharp):
    1.  
    2.  
    3. void Update()
    4.     {
    5.         Vector3 dir = (player.position - enemy.position).normalized;
    6.         Vector3 worldDir = (planet.position - enemy.position).normalized;
    7.  
    8.         float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
    9.         float angleX = Mathf.Atan(worldDir.x) * Mathf.Rad2Deg;
    10.  
    11.         enemy.position = Vector3.Slerp(enemy.position - planet.position, player.position - planet.position, Time.deltaTime * 0.4f); //il nemico segue il player sullla superficie sferica
    12.  
    13.         //Quaternion dirWorld = Quaternion.FromToRotation(enemy.transform.up, dir) * enemy.transform.rotation * Quaternion.FromToRotation(enemy.transform.up, worldDir) * enemy.transform.rotation; //rotazione in base al mondo sferico
    14.         var dir1 = Quaternion.Euler(0, angle, 0) * Vector3.up;
    15.         dir1 = Quaternion.Euler(-angleX, 0,0) * dir1;
    16.         enemy.rotation = Quaternion.FromToRotation(enemy.transform.up, dir1)*enemy.rotation;
    17.        
    18.  
    19.     }
    20.  
    21.  
    22.  
    23.  
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    But what exactly is the question you're asking?