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

Find two different rotation

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

  1. Bolt

    Bolt

    Joined:
    Dec 15, 2012
    Posts:
    296
    I made a game on planet.The enemy must follow the player so the forward must point to him. At the same time, it must rotate following the circumference. This is my script
    Code (csharp):
    1.  
    2. void Update()
    3.     {
    4.         Vector3 dir = (t2.position - t1.position).normalized;
    5.         Vector3 dirWorld = (planet.transform.position - t1.transform.position).normalized;
    6.        
    7.         float angle = Mathf.Atan2(dir.x, dir.z) * Mathf.Rad2Deg;
    8.         t1.transform.position = Vector3.Slerp(t1.transform.position - planet.position, t2.position - planet.position,Time.deltaTime*0.4f);
    9.         Quaternion dirW = Quaternion.FromToRotation(t1.up, dirWorld) * t1.rotation;
    10.         t1.rotation = Quaternion.Slerp(t1.transform.rotation, dirW, 50 * Time.deltaTime);
    11.        
    12.        
    13.     }
    14.  
    15.  
    16.