简体   繁体   中英

How can i enable my animations when I press a button without that it wait the other animation? | Mecanim

I am learning how to use the mecanim. In the animator I have 3 animations (idle,Walk and Run) these animations works correctly but the problem is when I press the button W, for example, two seconds pass until the animation (walk) is enabled, because that animation (walk) wait until the other animation (idle) finish. I want the animations are activated when pressed the button. How can I do that?

I think you have done your basics right,

1. Create "animator controller" then set your animator control's animations and get their boolean values , for ex: for jumping state get bool value as jumping, and for sliding state get bool value as sliding.

2. Then you set those boolean variables in to transition arrows,from "Any State" state.

3. you have to add transitions to "AnyState", because from there on, its changes to other states easily.

在此处输入图片说明

4. Don't forget to tick these items in each of your animation motions,unless it won't correctly return back to state that you want to have,

在此处输入图片说明

5. This is a sample code,which i have implemented to control a runner.

Your problem is you haven't set a delay time, like this Invoke("stopJumping",0.01f); .This 0.01f is the delay which causes your button's reaction time.

 using UnityEngine;
    using System.Collections;

    public class CharacterMovement : MonoBehaviour {
    private Animator animator;
    private int lane;

        // Use this for initialization
        void Start () {
        lane =0;
        animator =GetComponent<Animator>();
        }

        // Update is called once per frame
        void Update () {
          if(Input.GetKeyDown(KeyCode.UpArrow)) //When you pressed UpArrow
          {
             animator.SetBool("jumping",true); //Activate jumping 
              Invoke("stopJumping",0.01f);     // state(because it's boolval
                                               // And invoke stopJumping                                                                                                
          }                                     //     method      
          if(Input.GetKeyDown(KeyCode.DownArrow))
          {
              animator.SetBool("sliding",true);
              Invoke("stopSliding",0.01f);
          }
          if(Input.GetKeyDown(KeyCode.LeftArrow))
          {if(lane > -1)    //when you slide left,switch lane
                             //to left
              lane--;     
          }
           if(Input.GetKeyDown(KeyCode.RightArrow))
          {if(lane < 1)
              lane++;     
          }
          Vector3 newPosition =transform.position;
          newPosition.x=lane;
          newPosition.y=0f;
          transform.position= newPosition;

        }

        void stopJumping()
        {
            animator.SetBool("jumping",false);
        }
        void stopSliding()
        {
            animator.SetBool("sliding",false);
        }   
    }

If you want to refer a tutorial,just check this , which i referred by my self.

I found the answer that I needed. There is a checkbox when you select the transition which is called "Has Exit Time". It is clicked, then you must remove the tick of the checkbox and the animations that you put on the Animator Controller will reproduce when you press the determinated button. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM