Search Unity

Problem with animation change on key press

Discussion in 'Animation' started by traktorius81, Aug 7, 2018.

  1. traktorius81

    traktorius81

    Joined:
    Aug 7, 2018
    Posts:
    2
    Hello, i'm beginner in unity, and i have a problem with changing animations on key press. "Žmogus" is idle, and when i press D the animation should change to "eina dešinėn" and when i release D it should go back to idle. The transition from "eina dešinėn" to idle works great, but the transition from id to "eina dešinėn" happens even without pressing D. Please help

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class judėjimas_animacija : MonoBehaviour {
    6.  
    7.     Vector3 jgreitis = Vector3.zero;
    8.     Vector3 jgreitis2 = Vector3.zero;
    9.     public Vector3 gravitacija;
    10.     public Vector3 xasis;
    11.     public float yasis;
    12.     private Rigidbody2D zmogeliukas;
    13.     private SpriteRenderer spriteRenderer;
    14.     public Animator anim;
    15.  
    16.     bool space;
    17.     bool nusileido;
    18.  
    19.     // Use this for initialization
    20.     void Start()
    21.     {
    22.         zmogeliukas = GetComponent<Rigidbody2D>();
    23.         anim = GetComponent<Animator>();
    24.     }
    25.     void Update()
    26.     {
    27.         if (Input.GetKey(KeyCode.D))
    28.             transform.position += xasis * Time.deltaTime;
    29.             anim.SetTrigger("einad");
    30.         if (Input.GetKeyUp(KeyCode.D))
    31.             anim.SetTrigger("einada");
    32.         if (Input.GetButtonDown("Jump"))
    33.             space = true;
    34.  
     
  2. traktorius81

    traktorius81

    Joined:
    Aug 7, 2018
    Posts:
    2
    This is a photo of animator
     

    Attached Files: