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. Dismiss Notice

Problem with Animation scrript!

Discussion in 'Scripting' started by Anwarus, Oct 2, 2014.

  1. Anwarus

    Anwarus

    Joined:
    Feb 5, 2014
    Posts:
    3
    Hello guys! I have a little problem with my script. When I'm moving my character only up and down or only left and right it works perfect. But when i move up and turn left for example animation doesn't change instantly.
    Can you look?
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class Movement : MonoBehaviour {
    6.  
    7.     public float speed = 3.0F;
    8.     private Vector2 movement = Vector2.zero;
    9.     private Animator animator;
    10.  
    11.    // Use this for initialization
    12.    void Start () {
    13.  
    14.         animator = this.GetComponent<Animator>();
    15.    
    16.    }
    17.    
    18.    // Update is called once per frame
    19.    void Update () {
    20.         movement = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
    21.         movement = transform.TransformDirection(movement) * speed;
    22.         transform.Translate(movement * Time.deltaTime);
    23.         var vertical = Input.GetAxis("Vertical");
    24.         var horizontal = Input.GetAxis("Horizontal");
    25.  
    26.  
    27.  
    28.  
    29.       if (horizontal > 0)
    30.      {
    31.        animator.SetInteger("Direction", 2);
    32.      }
    33.      else if (horizontal < 0)
    34.      {
    35.        animator.SetInteger("Direction", 4);
    36.      }
    37.      else if (vertical > 0)
    38.      {
    39.        animator.SetInteger("Direction", 1);
    40.      }
    41.      else if (vertical < 0)
    42.      {
    43.        animator.SetInteger("Direction", 3);
    44.      }
    45.       else
    46.       {
    47.      animator.SetInteger("Direction", 0);
    48.      }
    49.    
    50.    }
    51. }
    Thanks for help!
     
  2. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Make sure you got all the neccessary transitions and uncheck the 'is atomic' checkbox of the transitions that do not work instantly, because it prevents the transitions from being interrupted. These are my only ideas, i'm not very familiar with Mecanim.

    *EDIT You're also using 'GetAxis' which has a kind of smoothing-effect, this could also be a problem because it does not immediately resets to 0, it'll decrease so you might still be running into an if-statement before the desired if statement can be reached.