- Home /
Bullet shooting animation
I have a character, that have a "shoot" animation. The animation take some time, because the character have to aim first, and it "shoots" almost at the end of the animation. I want the bullet to be created at a special frame of the animation, so I have made this script, but it dont work
var bullet : Transform; var newBullet; var bullets : int; bullets = 30;
function FixedUpdate() {
if(Input.GetButtonDown("Fire1") && bullets > 0 && !animation.IsPlaying("walk")) {
animation.Stop("stand"); animation.CrossFade("shoot");
}
if(animation["shoot"].time == 86) { newBullet = Instantiate(bullet, GameObject.Find("Ref_Weapon").transform.position,transform.rotation); newBullet.rigidbody.AddForce(transform.forward * 20000); bullets--; print("if Done"); } }
Why is this not working? Is there something wrong with animation["shoot"].time == 86 ?
Answer by skovacs1 · Oct 18, 2010 at 08:53 PM
There are a couple of things that could be wrong. The simplest answer is that your FixedUpdate() is never called exactly when your animationState's time is equal to 86 seconds. Also, depending on your wrapmode, this could also make this only happen once. To fix this, you can check when the time is greater than 86 or something like that and/or changing your update step to make certain that FixedUpdate is called at the intervals you expect and/or using Update in stead of FixedUpdate.
This isn't really the best way to trigger this. You should look into AnimationEvents they are designed for exactly what you're describing, but note that you cannot modify animations that have been imported with a character, etc. For imported animations you can a) duplicate the animations and modify the duplicates (see this question) or b) create a separate animations (with your events) that you trigger to run at the same time on a separate layer. Option a) is the most practiced because option b) is not very extensible.
Hi. I'm new to this animation event stuff, and now I have read a little about it, and it looks smart. The problem is that the animations in animation event are read-only. What to do to avoid it? I cant mess around with the model, because its not made by me, and I cant work in 3d model programs.
There are a few ways to do this, but the thing to note is that you cannot modify imported animations directly. I will add information to this answer.
Hi again. I have some trouble understanding whats going on in the answer in the link you sent me. In the answer, he tells me what to do with steps, and gives me a piece of code. He tells me to put something in a editor folder, but what "editor" folder and what to put in the folder?? He also assumes that you already have made a copy of the animation, but how can I do that?
If you don't have an Editor folder in your project asset root, create one. You put the script in the folder - it's an editor script after all. The duplicate would just be a new animation clip on the gameobject named the_same_name_copy. You would then select the original animation clip in your project folder (should be stored in your root if you set your importer up that way) go to "Assets/Transfer Clip Curves to Copy" (that you just added with the script) and your clip has been duplicated.
Your answer