I have my enemy perform via state machine an animation whereby he raises his gun ready to fire.
Once the gun is raised and animation stopped I “intend” to then add the bullet game object at the front of the gun and perform the code to make it fire forward.
The question is if I include another animation that create a flash and slight recoil of the gun how do you go about making this happen at the same time the bullet code fires?
I’d add an animation event at the right point in the raise-and-aim animation. The event can call your Fire() method:
void Fire() {
GetComponent<Animator>().Play("Kaboom");
// (Instantiate bullet and give it velocity here)
// Maybe also play an audioclip.
}
You could even make the raise-and-aim and flash/recoil part of the same animation clip, and add the animation event at the point in the middle of the clip where the bullet fires. This way you don’t need to call Animator.Play to play another clip.
Side note: You can’t add animation events to animation clips that are part of a model. Expand the model file, select the clip, and press Ctrl+D to duplicate the clip to its own asset file. You can then add the animation event to that asset file.
I think what you are looking for are animation events. You want to add an event at the end of your “ready” stance animation, and then proceed to roll into your firing animation. Your firing animation would probably have its own animation events as well.