Search Unity

Grid movement, animations not playing correctly

Discussion in '2D' started by Astamor, Oct 4, 2018.

  1. Astamor

    Astamor

    Joined:
    Oct 4, 2018
    Posts:
    3
    So I've been working on this movement system based on grid. You can find similar system in games like Tibia or old pokemon. Basically your character can only move between tiles. Movement works fine but animations don't. When I hold a key the walking animation is played correctly but when I tap movement button once it doesn't start playing animation, it just slide my character to the next tile while displaying idle animation. What I would like to achieve is if I tap for example key "A" once, the character begin to move to the next tile but also playing walking animation until it reaches destination and stops, then it begins playing idle animation .Here is my code:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerController : MonoBehaviour {
    6.  
    7.     float speed = 2.0f;
    8.     Vector3 pos;
    9.     private Animator anim;
    10.     private bool playerMoving;
    11.     private Vector2 lastMove;
    12.    
    13.  
    14.     // Use this for initialization
    15.     void Start () {
    16.  
    17.         playerMoving = false;
    18.         anim = GetComponent<Animator>();
    19.         pos = transform.position;
    20.     }
    21.  
    22.     // Update is called once per frame
    23.     void FixedUpdate() {
    24.  
    25.         if(Input.GetAxisRaw("Horizontal") == 0f && Input.GetAxisRaw("Vertical") == 0f)
    26.         {
    27.             playerMoving = false;
    28.         }
    29.  
    30.         if (Input.GetAxisRaw("Horizontal") < -0.001f && transform.position == pos)
    31.         {
    32.             pos += Vector3.left;
    33.             playerMoving = true;
    34.             lastMove = new Vector2(Input.GetAxisRaw("Horizontal"), 0f);
    35.         }
    36.  
    37.         else if (Input.GetAxisRaw("Horizontal") > 0.001f && transform.position == pos)
    38.         {
    39.             pos += Vector3.right;
    40.             playerMoving = true;
    41.             lastMove = new Vector2(Input.GetAxisRaw("Horizontal"), 0f);
    42.         }
    43.         else if (Input.GetAxisRaw("Vertical") > 0.001f && transform.position == pos)
    44.         {
    45.             pos += Vector3.up;
    46.             playerMoving = true;
    47.             lastMove = new Vector2(0f, Input.GetAxisRaw("Vertical"));
    48.         }
    49.         else if (Input.GetAxisRaw("Vertical") < -0.001f && transform.position == pos)
    50.         {
    51.             pos += Vector3.down;
    52.             playerMoving = true;
    53.             lastMove = new Vector2(0f, Input.GetAxisRaw("Vertical"));
    54.         }
    55.  
    56.  
    57.        
    58.  
    59.         anim.SetFloat("MoveX", Input.GetAxisRaw("Horizontal"));
    60.         anim.SetFloat("MoveY", Input.GetAxisRaw("Vertical"));
    61.         transform.position = Vector3.MoveTowards(transform.position, pos, Time.deltaTime * speed);
    62.         anim.SetBool("PlayerMoving", playerMoving);
    63.         anim.SetFloat("LastMoveX", lastMove.x);
    64.         anim.SetFloat("LastMoveY", lastMove.y);
    65.     }
    66. }
    67.  
     
  2. lipisis

    lipisis

    Joined:
    Jan 14, 2017
    Posts:
    37
    The problem is that you check for axis input every frame. So when you press a button, everything works alright but when you release that button, your first if becomes true and playermoving becomes false and your animation stops.

    You might want to check if object reached his destination or not. If not, then don't set playerMoving to false
     
    Astamor likes this.
  3. Astamor

    Astamor

    Joined:
    Oct 4, 2018
    Posts:
    3
    Ok, I've checked that with this:
    Code (CSharp):
    1. if(Input.GetAxisRaw("Horizontal") == 0f && Input.GetAxisRaw("Vertical") == 0f)
    2.         {
    3.             if(pos == transform.position)
    4.             {
    5.                 playerMoving = false;
    6.             }
    and now if I tap button once it no longer slide character to the next tile while displaying idle animation. Now it plays walking animations but if I tap button once, no matter what direction it awlays plays "walk_up" animation.
     
  4. Astamor

    Astamor

    Joined:
    Oct 4, 2018
    Posts:
    3
    Ok, problem solved, apparently it can't be done with blend tree. Only idle movement is a blend tree, walking animations had to be separated and now it works perfectly.