Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Sirmaiji · Sep 01, 2013 at 12:56 AM · animationshootingbulletgunshoot

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 :)

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2

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.

Comment
Add comment · Show 9 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Sirmaiji · Sep 01, 2013 at 01:47 PM 0
Share

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?

avatar image SudoSandwich · Sep 01, 2013 at 06:25 PM 0
Share

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.

avatar image Sirmaiji · Sep 01, 2013 at 08:17 PM 0
Share

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?

avatar image SudoSandwich · Sep 01, 2013 at 08:59 PM 0
Share

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.

avatar image Sirmaiji · Sep 01, 2013 at 09:49 PM 0
Share

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...

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges