- Home /
Shooting & Animation Problem
I've got shooting script, here is a part of it:
var Bullet : Transform; var Speed = 500; var spawnPoint : Transform; var RapidFire = true; var SingleFire = false;
//RateOfFire private var Counter = Time.deltaTime; var RateOfFire = 0.250000;
function FixedUpdate () {
if
(RapidFire ==true){
if
(Input.GetButtonDown("Action") && Input.GetButton("Aiming")){
shooting=true;
animation.CrossFade("shoot");
}
if
(Input.GetButtonUp("Action") && Input.GetButton("Aiming")){
shooting=false;}
if
(shooting==true){ Counter += Time.deltaTime;
if
(RateOfFire < Counter){
var shotRapid =Instantiate(Bullet, GameObject.Find("spawnPoint").transform.position, Quaternion.identity);
//speed
shotRapid.rigidbody.AddForce(transform.forward * Speed);
Counter=0; }
}
}
}
The question is how to synchronize shooting with my animation. It's 3rd person shooter I'm working at and I've got my character animation being looped. The shot itself should be performed approximately at 0.58 second after pressing shoot button and animation get started. After that gun recoil follows within animation and exactly before that a shot must be performed. I wanna make a bullet shot depending on animation timeline's position. Nonetheless it's hand gun I've decided to make it fire all the time on a low firing rate, when "shooting" button's pressed. Using this script I can control firing rate, but it still won't give me exact synchronization with the animation. So the thing I wanna achieve most is to make shooting action become dependent on my "shoot" animation played. And one more - still can't get bullets fly straight forward the direction of a gun (if I shoot upwards it still shoots forward). But the script seem to be all right. Thanks in advance for your help :)
Answer by SudoSandwich · Sep 01, 2013 at 03:19 AM
If you created your animation in unity you can right click the top portion of the timeline (top half of the light grey area). From here you can make a call to a function in a script attached to the object.
For example I have an axe weapon and I make a call to attackDamage() so that the axe hits the player then causes damage so when the player health is < 0 he does not start falling before being struck.
Thanks for your answer, it seem starting to help. But could you please write a short example? There's only FixedUpdate() function to choose in pop-up list in animation window for adding animation events. I don't get what I should do next. Let's say I wanna start bullet shoot at a frame 20. So I gotta create animation event on timeline at frame 20. Now I need something to do with my script attached to animated object. So what exactly should I do next?
You should put the bullet action within a function. That way it will show up on the timeline. I will post an example in just a sec.
From what you have there it looks like these lines should be placed in a function.
var shotRapid=Instantiate(Bullet,GameObject.Find("spawnPoint").transform.position, Quaternion.identity);
shotRapid.rigidbody.AddForce(transform.forward * Speed);
That way you can make a call to instantiate the bullet at the given frame and move it.
Thank you so much for your assistance! It helped a lot :)But still one issue remains with those animations. I've got each separate animation for each shooting type. F.e. "shoot", "shoot_up" and "shoot_down". They are all suppose to be played in CrossFade mode when, let's say, button "Action" is pressed. But when I'm changing my character's arms' position according to each shooting type holding "Action button" one extra shot takes place in transition between, let's say, up and forward shooting position. Why it could be so?
That is odd. Sounds like something might be happening in your code. I am not sure if you can debug in unity the way you can for say c/c++ where you can step through the code to see the animations at each line.
I would put debug.log("TEXT HERE") statements throughout the code when shots take place. Then when you switch you can see which one is getting called to narrow it down. Print statements are you friend when it comes to troubleshooting.
It seems I got what the problem is. As I use "CrossFade" mode for all of my animation types here comes the problem: I've got also 3 ai$$anonymous$$g animations (animated poses) for each type of shooting action accordingly. So while "Action" button's pressed and I press "up" button to switch to another pose (for ai$$anonymous$$g up) it starts shooting animation instantly before "raising arms up" animation played till the end. I guess it should be kinda delay function or smthn like that for next shooting (shooting up) animation being started, but only after "raising arms up" animation reaches it's end...
Your answer
Follow this Question
Related Questions
How to Sync bullet spawn with shoot animation 0 Answers
Help with getting a bullet to move. 2 Answers
Bullet does not move forward 1 Answer
Shooting script not working properly 3 Answers
delay between shots 1 Answer