- Home /
Do Something Relative to Animation Time
I have 2d firing animation. I want to create and move the bullets towards enemy and I want to make this happen when at a certain time of the animation. I can access to animation time by using:
soldierAnimator.runtimeAnimatorController.animationClips[1].length
Also I am using coroutine to do what I want. Is there any easier or effective way to do this?
void fireTo(Enemy e){
soldierAnimator.SetBool("isReloaded", true);
soldierAnimator.SetBool("isInRange", true);
StartCoroutine("waitIt", e);
}
IEnumerator waitIt(Enemy e){
yield return new WaitForSeconds(0.5f);
GameObject bulletGO = (GameObject)Instantiate(bulletPrefab, this.transform.position, this.transform.rotation);
Bullet b = bulletGO.GetComponent<Bullet>();
if(e != null) {
b.getTarget = e.transform;
}else{
DestroyObject(b);
}
}
Answer by jdean300 · Jun 17, 2016 at 09:42 PM
Animation events. I always get to these by looking in the animator graph and double clicking a state. In the inspector you will see various options for that animation state. Towards the bottom is an events area where you can add an event - youll need to give it a script and a function name to look for on the object playing the animation. You can set the event to run at any point in the animation you want.
Thanks for the answer when I try it I will write my results.
Your answer
Follow this Question
Related Questions
2D Animation does not start 1 Answer
How to prevent holding down a key to infinitely loop an action? 1 Answer
Line of code skips randomly at times? 1 Answer
Sprite animation 2 Answers
Coroutine is not working 2 Answers