- Home /
Animation when the key is down and second animation when the key is up
I'm trying to make an animation1 which starts when a key is held down and stays at its end until the same key is up. When that key is up a second animation2 starts. Also, if I press down and up the key the object performs the two animations one after the other. Also, if I press the key down when animations are performing, the animation1 mustn't start again (animation2 only starts when animation1 is over). The system I'm trying to implement is meant for a sword that when you hit a key down the sword performs a slash and stays at that position until the key is up and the sword performs a second slash that leads it to the original position.
What I've done so far:
Transitions amng the animations and an Idle state:
The script that controls everything:
Animator anim;
void Start () {
anim = GetComponent<Animator> ();
}
void Update () {
if (Input.GetKeyDown (KeyCode.Space) && anim.GetBool("condition1")==false)
anim.SetBool("condition1",true);
if (Input.GetKeyUp (KeyCode.Space) && anim.GetBool ("condition2") == false) {
anim.SetBool ("condition2", true);
anim.SetBool("condition1",false);
}
}
public void Event1(){
anim.SetBool ("condition1", false);
}
public void Event2(){
anim.SetBool ("condition2", false);
}
I've put events at the end of animation1 and animation2 which call Event1() and Event().
PROBLEMS: If I check Has Exit Time to all transitions and put all settings to 0 it works as expected but there are lags between animations and sometimes when I press down and up the key it does nothing and I have to press the key again. Where the lags come from? If I uncheck Has Exit Time boxes there are no lags but when the key is up animation won't finish and animation2 starts without animation1 being over. I'm stucked at this point.
I don't know if I explained myself well because English is not my native language, but I tried to give all the details that I know. I'm trying to understand all Mecanim system before trying to do more complex animations. Thanks.