Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Improve movement and animation

Discussion in '2D' started by TheSteiner, Oct 17, 2017.

  1. TheSteiner

    TheSteiner

    Joined:
    Dec 14, 2016
    Posts:
    6
    I'm working on the basis of my next 2D project and as I'm new to Unity I'm having some problems.

    One of them is with animation and movement. I leave a video for you to see:



    The change of animation with the movement is not completely fluid, for example, when moving upwards and then to the left the character continues to look briefly upwards, instead of immediately executing the animation of walking to the left.

    This is the script I am using:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class playerController : MonoBehaviour
    6. {
    7.     private Animator animator;
    8.     public float speed;
    9.     // Use this for initialization
    10.     void Start()
    11.     {
    12.         animator = this.GetComponent<Animator>();
    13.     }
    14.     // Update is called once per frame
    15.     void Update()
    16.     {
    17.         var vertical = Input.GetAxis("Vertical");
    18.         var horizontal = Input.GetAxis("Horizontal");
    19.        
    20.                   if (Input.GetKey (KeyCode.D))
    21.          {
    22.              transform.Translate(Vector2.right * speed);
    23.          }
    24.        
    25.          if (Input.GetKey (KeyCode.A))
    26.          {
    27.              transform.Translate(-Vector2.right * speed);
    28.          }
    29.          if (Input.GetKey (KeyCode.W))
    30.          {
    31.              transform.Translate(Vector2.up * speed);
    32.          }
    33.          if (Input.GetKey (KeyCode.S))
    34.          {
    35.              transform.Translate(-Vector2.up * speed);
    36.          }
    37.         if (vertical > 0)
    38.         {
    39.             animator.SetInteger("Direction", 0);
    40.         }
    41.         else if (vertical < 0)
    42.         {
    43.             animator.SetInteger("Direction", 2);
    44.         }
    45.         else if (horizontal > 0)
    46.         {
    47.             animator.SetInteger("Direction", 1);
    48.         }
    49.         else if (horizontal < 0)
    50.         {
    51.             animator.SetInteger("Direction", 3);
    52.         }
    53.  
    54.     }
    55. }
    Thanks.
     
  2. RuneShiStorm

    RuneShiStorm

    Joined:
    Apr 28, 2017
    Posts:
    264
    Your script looks ok, not that Im an expert but..
    I used this guys tutorials for my own game, it might be helpful..
    Here is a link to one of his 2D RPG.. Same style as your game
     
    TheSteiner likes this.
  3. Sluggy

    Sluggy

    Joined:
    Nov 27, 2012
    Posts:
    960
    My guess would be that either your input isn't setup properly in the input manager or your animator transition isn't setup properly for your sprite. Check to make sure you have a high acceleration and gravity set for your input and that the deadzone isn't too big. Also check to make sure your animator's transition to that state has exit time turned off and that the transition time is also set to 0.
     
    TheSteiner likes this.
  4. TheSteiner

    TheSteiner

    Joined:
    Dec 14, 2016
    Posts:
    6
    I finally solved it. It was the "Sensitivity" attribute in the Input Manager. By reducing the value I have managed to make the change of direction and animation immediate.

    Thank you!