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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Simple way to look movement direction

Discussion in 'Scripting' started by jsull1, May 18, 2018.

  1. jsull1

    jsull1

    Joined:
    Apr 3, 2018
    Posts:
    121
    In my game I have both characters and enemies who need to look the direction they move in. It is in almost every game so I don't know why I'm struggling so hard to find the correct way to do this. This is my player script:

    Code (CSharp):
    1. public float speed;
    2.     private float MoveVertical;
    3.     private float MoveHorizontal;
    4.  
    5.     void Update () {
    6.    
    7.         MoveHorizontal = Input.GetAxis ("Horizontal");
    8.         MoveVertical = Input.GetAxis ("Vertical");
    9.         Vector2 Movement = new Vector2 (MoveHorizontal, MoveVertical);
    10.         GetComponent<Rigidbody2D>().velocity =  Movement * speed;
    and this is the enemy:

    void Update () {
    enemydistanceX = Mathf.Abs (target.position.x - transform.position.x);
    enemydistanceY = Mathf.Abs (target.position.y -transform.position.y);
    if (enemydistanceX < enemydistanceallowance && enemydistanceY < enemydistanceallowance){
    transform.up = -target.position - transform.position;
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    This is some code for 8-directional movement with facing the direction of movement..
    Code (csharp):
    1. public class Test3 : MonoBehaviour
    2. {
    3.     [SerializeField]
    4.     Rigidbody2D rb;
    5.     public float speed = 2;
    6.     void Update()
    7.     {
    8.         float h = Input.GetAxisRaw("Horizontal");
    9.         float v = Input.GetAxisRaw("Vertical");
    10.  
    11.         Vector3 move;
    12.         move.x = h;
    13.         move.y = v;
    14.         move.z = 0;
    15.         move.Normalize();
    16.  
    17.         if (h != 0 || v != 0) transform.right = move;
    18.  
    19.         move *= speed;
    20.         rb.velocity = move;
    21.     }
    22. }
    Hope that helps some.
     
    Doug_B and jsull1 like this.
  3. jsull1

    jsull1

    Joined:
    Apr 3, 2018
    Posts:
    121
    Wow! That's brilliant! thank you! but secondary question, how can I make it more fluid? Also what is the purpose of Normalize here?
     
  4. jsull1

    jsull1

    Joined:
    Apr 3, 2018
    Posts:
    121
    Also why does your 'if' statement not need curly brackets after, don't they always go
    If (true of false statement){thing that happens}
     
  5. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    When there is only 1 statement, the braces are optional.

    It means if you had 1 up and 1 left, it would make the magnitude only 1 (so you wouldn't go faster diagonally, for instance).

    As for how to make it more fluid, that depends on if you mean:
    1) it gradually turns
    2) you can face more than 8 directions.

    For 1, you can store the rotation/direction in a variable, and gradually turn towards it over time.
    For 2, you'd have to alter the code somewhat. It could be several variations and I can't just write it up right this second..

    :)