- Home /
Stop animation after .5 seconds?
Hi there!
When pressing E, the player starts an Attack animation. That needs to stop after 0.5 seconds.
How do I do that?
The simple code looks like this:
if(Input.GetKey(KeyCode.E))
{
anim.SetBool("Attack", true);
Instantiate(meleePrefab, new Vector3(target.position.x+1,transform.position.y,target.position.z), transform.rotation);
}
Thanks
This might suck, but you can try it! I don't script with animations much:
if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.E))
{
anim.SetBool("Attack", true);
Instantiate(meleePrefab, new Vector3(target.position.x+1,transform.position.y,target.position.z), transform.rotation);
yield return new WaitForSeconds(0.5);
anim.SetBool("Attack", false);
}
Basically just tells the script to pause for half a second (during which time the animation will be playing) then to stop the animation. Let me know if it works! If it doesn't, I'll take down my answer so you're more likely to get another one.
Yeah you'd probably need to do a coroutine.
I already tried this, didn't work :/ I might be wrong, but isn't the yield function only for IEnumerators ?
It's possible that that's true. $$anonymous$$aybe you need to find another way to start the animation. I think if you make it a legacy animation ins$$anonymous$$d, I think there is a line that calls only a certain portion of the animation, rather than set a Bool to True.
But, that probably won't work. $$anonymous$$aybe you could duplicate the actual animation file, then go into the file and edit the animation clip to only include that 0.5 seconds of animation. Then just use that new clip of 0.5 seconds as the animation to set to true.
You need to specify which language you are using so we can be more helpful.
Answer by Addyarb · Jul 18, 2014 at 07:40 PM
void Update(){
if(Input.GetKey(KeyCode.E))
{
anim.SetBool("Attack", true);
StartCoroutine(WaitForHalfASecond());
Instantiate(meleePrefab, new Vector3(target.position.x+1,transform.position.y,target.position.z), transform.rotation);
}
}
IEnumerator WaitForHalfASecond(){
yield return new WaitForSeconds(0.5);
anim.SetBool("Attack", false);
}
C#
If you have any questions, or would like an explanation on how to further use IEnumerators in this case, I will be happy to answer in the comments. IEnumerating is a difficult concept to grasp until you've done it a lot!
Your answer
Follow this Question
Related Questions
GetKey/GetKeyDown, Paired with Adjusting Stats 1 Answer
Can/how do I set up a boolean to continue an animation after button press 1 Answer
How to get holding key to prioritize 1 Answer
Check if key is pressed once 1 Answer
How to stop repeated letter/backspace entry in an input field when they key is held down? 1 Answer