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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Grid movement animation

Discussion in '2D' started by Jihaysse, Apr 29, 2020.

  1. Jihaysse

    Jihaysse

    Joined:
    Mar 29, 2020
    Posts:
    53
    Hello everyone,

    I've tried to make my current Player Controller script to make the player moves from tile to tile.
    It works well, except for the animation. I tried many ways but I can't change these correctly.
    Here's what happening: the animation changes (up, down, right, left) as soon as I press the corresponding arrow key, no matter if my player is already on the next tile or not.
    I would like the animation to changes ONLY when the player has reached the next tile.

    I can make a video if it's unclear.

    Here is a part of my current script (sorry if it's a mess hehe):
    The "movePoint" is the object which moves tiles to tiles.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5.  
    6. [RequireComponent(typeof(Rigidbody2D))]
    7.  
    8. public class PlayerController : MonoBehaviour {
    9.  
    10.     public float moveSpeed = 5f;
    11.     public float m_MovementSmoothing = 0.1f;
    12.     public Transform movePoint;
    13.     public GameObject upObject;
    14.     public GameObject leftObject;
    15.     public GameObject rightObject;
    16.     public GameObject downObject;
    17.     Transform Player;
    18.  
    19.     public static PlayerController instance;
    20.  
    21.    
    22.     public bool canMove = true;
    23.    
    24.  
    25.     Rigidbody2D rb;
    26.     Animator currentAnimator;
    27.  
    28.     enum Direction { Up, Right, Down, Left };
    29.    
    30.  
    31.     Direction currentDirection;
    32.     Direction previousDirection;
    33.     float angle = 180;
    34.     float speed;
    35.  
    36.     Vector2 axisVector = Vector2.zero;
    37.     Vector3 currentVelocity = Vector3.zero;
    38.  
    39.     void Start()
    40.     {
    41.        
    42.         rb = GetComponent<Rigidbody2D>();
    43.         upObject.SetActive(false);
    44.         leftObject.SetActive(false);
    45.         rightObject.SetActive(false);
    46.         downObject.SetActive(true);
    47.  
    48.         currentAnimator = downObject.GetComponent<Animator>();
    49.  
    50.         movePoint.parent = null;
    51.  
    52.        
    53.     }
    54.  
    55.     void Update()
    56.     {
    57.         if (canMove)
    58.         {
    59.  
    60.             transform.position = Vector3.MoveTowards(transform.position, movePoint.position, moveSpeed * Time.deltaTime);
    61.  
    62.             if (Vector3.Distance(transform.position, movePoint.position) <= 0f)
    63.             {
    64.                 if (Input.GetAxisRaw("Horizontal") != 0)
    65.                 {
    66.                     rb.velocity = new Vector2(Input.GetAxisRaw("Horizontal"), 0f) * moveSpeed;
    67.                     movePoint.position += 2 * (new Vector3(Input.GetAxisRaw("Horizontal"), 0f, 0f));
    68.  
    69.                 }
    70.  
    71.  
    72.                 else if (Input.GetAxisRaw("Vertical") != 0)
    73.                 {
    74.                     rb.velocity = new Vector2(0f, Input.GetAxisRaw("Vertical")) * moveSpeed;
    75.                     movePoint.position += 2 * new Vector3 (0f, Input.GetAxisRaw("Vertical"), 0f);
    76.  
    77.                 }
    78.  
    79.  
    80.                 else
    81.                     rb.velocity = Vector2.zero;
    82.             }
    83.            
    84.         }
    85.  
    86.         else
    87.         {
    88.             rb.velocity = Vector2.zero;
    89.         }
    90.         // get speed from the rigid body to be used for animator parameter Speed
    91.         speed = 5 * rb.velocity.magnitude;
    92.  
    93.         // Get input axises
    94.         axisVector = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
    95.  
    96.    
    97.         // Find out which direction to face and do what is appropiate
    98.         //
    99.  
    100.         // Only update angle of direction if input axises are pressed
    101.         if (!(axisVector.x == 0 && axisVector.y == 0))
    102.         {
    103.             // Find out what direction angle based on input axises
    104.             angle = Mathf.Atan2(axisVector.x, axisVector.y) * Mathf.Rad2Deg;
    105.  
    106.             // Round out to prevent jittery direction changes.
    107.             angle = Mathf.RoundToInt(angle);
    108.         }
    109.  
    110.  
    111.         if (angle > -45 && angle < 45)  // UP
    112.         {
    113.             currentDirection = Direction.Up;
    114.         }
    115.  
    116.         else if (angle < -135 || angle > 135) // DOWN
    117.         {
    118.             currentDirection = Direction.Down;
    119.         }
    120.  
    121.         else if (angle >= 45 && angle <= 135) // RIGHT
    122.         {
    123.             currentDirection = Direction.Right;
    124.         }
    125.  
    126.         else if (angle <= -45 && angle >= -135)  // LEFT
    127.         {
    128.             currentDirection = Direction.Left;
    129.         }
    130.  
    131.         // Did direction change?
    132.         if (previousDirection != currentDirection)
    133.         {
    134.  
    135.             if (currentDirection == Direction.Up)
    136.             {
    137.                 // Activate appropiate game object
    138.                 upObject.SetActive(true);
    139.                 rightObject.SetActive(false);
    140.                 leftObject.SetActive(false);
    141.                 downObject.SetActive(false);
    142.  
    143.                 currentAnimator = upObject.GetComponent<Animator>();
    144.             }
    145.  
    146.             else if ( currentDirection == Direction.Down)
    147.             {
    148.                 // Activate appropiate game object
    149.                 upObject.SetActive(false);
    150.                 rightObject.SetActive(false);
    151.                 leftObject.SetActive(false);
    152.                 downObject.SetActive(true);
    153.  
    154.                 currentAnimator = downObject.GetComponent<Animator>();
    155.             }
    156.  
    157.             else if (currentDirection == Direction.Right)
    158.             {
    159.                 // Activate appropiate game object
    160.                 upObject.SetActive(false);
    161.                 rightObject.SetActive(true);
    162.                 leftObject.SetActive(false);
    163.                 downObject.SetActive(false);
    164.  
    165.                 currentAnimator = rightObject.GetComponent<Animator>();
    166.             }
    167.  
    168.             else if (currentDirection == Direction.Left)
    169.              {
    170.                 // Activate appropiate game object
    171.                 upObject.SetActive(false);
    172.                 rightObject.SetActive(false);
    173.                 leftObject.SetActive(true);
    174.                 downObject.SetActive(false);
    175.  
    176.                 currentAnimator = leftObject.GetComponent<Animator>();
    177.              }
    178.  
    179.  
    180.  
    181.         }
    182.  
    183.         // Set speed parameter to the animator
    184.         currentAnimator.SetFloat("Speed", speed);
    185.  
    186.         // Set current direction as previous
    187.         previousDirection = currentDirection;
    188.  
    189.  
    190.  
    191.     }
    192.  
    193.  
    194. }
    195.  
    Thank you for your time & your help! :)
     
  2. nabrown

    nabrown

    Joined:
    Jun 27, 2019
    Posts:
    27
    Are you using a different animator for each animation? For example, this line makes it look like you have a different animator setup for each direction?

    currentAnimator = upObject.GetComponent<Animator>();

    If that's the case I would definitely recommend going back and converting to use a single animator with multiple animations. Then you can use animator parameters to change between the animations based on your velocity. As for changing the animation only when you reach a new tile, you'd likely need some sort of trigger collider setup to toggle a boolean animation parameter whenever your character touches it.