Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

C# Need Help Tweaking 2d rotation

Discussion in 'Scripting' started by kenaochreous, Feb 19, 2015.

  1. kenaochreous

    kenaochreous

    Joined:
    Sep 7, 2012
    Posts:
    395
    I have been trying to fix the rotation for my steering script. I'm trying to get the scripted 2d gameobject to always face the target and while I was able to get it to sort of look at the target it's not looking directly at it. Do you guys have an suggestions for tweaks I could make?

    Code (CSharp):
    1.     void Seek (){
    2.         Vector3 direction = target.position - transform.position;
    3.         float moveHorizontal =  transform.position.x - target.position.x;
    4.         float moveVertical =  transform.position.y - target.position.y;  
    5.                        
    6.             float zRotation = Mathf.Rad2Deg * Mathf.Atan2(moveVertical, moveHorizontal);
    7.  
    8.             transform.eulerAngles = new Vector3(0, 0, zRotation + 50);
    9.        if(direction.magnitude > minDistance){
    10.             Vector3 moveVector = direction.normalized * moveSpeed * Time.deltaTime;
    11.           transform.position += moveVector;
    12.    
    13.        }
    14.     }
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    8,937
  3. kenaochreous

    kenaochreous

    Joined:
    Sep 7, 2012
    Posts:
    395