Search Unity

Getting a moving sprite to face the direction of movement

Discussion in '2D' started by KunoNoOni, Nov 24, 2015.

  1. KunoNoOni

    KunoNoOni

    Joined:
    Nov 16, 2013
    Posts:
    27
    I have a simple sprite that follows several waypoints automatically, the problem is I cannot get the sprite to rotate towards the next waypoint. I've tried searching google and have tried all sorts of different ideas. Not one of them has worked.

    Here is my waypoint movement code

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class MoveEnemy : MonoBehaviour
    6. {
    7.     public Transform[] waypoints;
    8.     public float speed;
    9.  
    10.     int waypointIndex;
    11.     int endPoint;
    12.  
    13.     Enemy1 enemy1;
    14.    
    15.     void Start ()
    16.     {
    17.         waypointIndex = 0;
    18.         endPoint = waypoints.Length - 1;
    19.         enemy1 = GetComponent<Enemy1>();
    20.     }
    21.  
    22.     void Update ()
    23.     {
    24.         MoveToWaypoint();
    25.     }
    26.    
    27.     void MoveToWaypoint()
    28.     {
    29.         Vector3 dir = waypoints[waypointIndex].position - this.transform.position;
    30.         if(dir.magnitude > .05f)
    31.         {
    32.             this.transform.Translate( dir.normalized * speed * Time.deltaTime );
    33.         }
    34.         else
    35.         {
    36.             if(waypointIndex == endPoint)
    37.                 enemy1.Die();
    38.             else
    39.                 waypointIndex++;
    40.         }
    41.     }
    42. }
    43.  
    The sprite will start at the top of the screen and initially will move down before move back towards the top of the screen. Its initial facing is down.

    The waypoints are located at:

    1. 1,3,0
    2. 2,0,0
    3. 4,-2,0
    4. 6,0,0
    5. 4,3,0
    6. 4,6,0
     
  2. enhawk

    enhawk

    Joined:
    Aug 22, 2013
    Posts:
    833
  3. LiberLogic969

    LiberLogic969

    Joined:
    Jun 29, 2014
    Posts:
    138
    Something like this could do it.

    Code (CSharp):
    1. void MoveToWaypoint()
    2.     {
    3.         Vector3 dir = waypoints[waypointIndex].position - this.transform.position;
    4.         if(dir.magnitude > .05f)
    5.         {
    6.             this.transform.Translate( dir.normalized * speed * Time.deltaTime );
    7.             targetRot = Mathf.Atan2(dir.normalized.y, dir.normalized.x) * Mathf.Rad2Deg;
    8.             transform.rotation = Quaternion.Euler(new Vector3(0, 0, targetRot));
    9.         }
    10.         else
    11.         {
    12.             if(waypointIndex == endPoint)
    13.                 enemy1.Die();
    14.             else
    15.                 waypointIndex++;
    16.         }
    17.     }
     
    Last edited: Nov 25, 2015
  4. KunoNoOni

    KunoNoOni

    Joined:
    Nov 16, 2013
    Posts:
    27
    @hawken Already tried LookAt and it didn't work. When I run the code, the sprite rotates on all 3 axis.

    @LiberLogic969 This didn't work either, the sprite made an arc as it visited each waypoint.

    I appreciate both of your suggestions.
     
  5. LiberLogic969

    LiberLogic969

    Joined:
    Jun 29, 2014
    Posts:
    138
    That is strange. The rotation should just snap towards the next point immediately. Have you experimented with the different overloads of the Translate method?

    Code (CSharp):
    1. transform.Translate(dir.normalized * speed * Time.deltaTime, Space.World);
    2.  
    3. // OR
    4.  
    5. transform.Translate(dir.normalized * speed * Time.deltaTime, Space.Self);
    You may also want to try simply adding your enemy sprite to a child of the main enemy GameObject and hold a reference to it in your script, then rotate its transform instead of the whole enemy GameObject, something like this :

    Code (CSharp):
    1.  
    2.  
    3. public Transform SpriteContainer;
    4.  
    5. void MoveToWaypoint()
    6.         {
    7.             Vector3 dir = waypoints[waypointIndex].position - this.transform.position;
    8.             if(dir.magnitude > .05f)
    9.             {
    10.                 this.transform.Translate( dir.normalized * speed * Time.deltaTime );
    11.  
    12.                 targetRot = Mathf.Atan2(dir.normalized.y, dir.normalized.x) * Mathf.Rad2Deg;
    13.                 SpriteContainer.rotation = Quaternion.Euler(new Vector3(0, 0, targetRot));
    14.             }
    15.             else
    16.             {
    17.                 if(waypointIndex == endPoint)
    18.                     enemy1.Die();
    19.                 else
    20.                     waypointIndex++;
    21.             }
    22.         }
    23.  
    Sometimes this could solve issues like the one you are having, in my experience at least.
     
    Last edited: Nov 26, 2015
  6. KunoNoOni

    KunoNoOni

    Joined:
    Nov 16, 2013
    Posts:
    27
    Code (CSharp):
    1. transform.Translate(dir.normalized * speed * Time.deltaTime, Space.World);
    This worked! Thank you so much!