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 Crumpet · Mar 25, 2014 at 05:04 AM · c#animationsfirereloadfinish

finishing an animation before starting another?

I use this script to make my gun fire and while it's firing, the gun can also play the reload animation and while it's reloading the gun will also be able to shoot. I wanted to create a way of checking if the gun has finished reloading or firing? is there any way of doing this? I googled a bit to try and find something but nothing really matched what I wanted to do. also, yes my script is probably messy I'm new to programming.

 using UnityEngine;
 using System.Collections;
 
 public class WeaponFire : MonoBehaviour
 {
     public bool outofammo;
     public int bullets = 7;
     public GameObject deagle; //this is my gun
     public float time = 1;
     public float firerate = 1;
     public bool fired = false;
     public bool Canfire = true;
     public bool timedown;
     Transform Effect;
     public float TakeDamage = 100;
 //    object particleClone;
     RaycastHit hit;
     public float Distance;
     public float MaxDistance = 2;
     public int Damage = 25;
     public bool reloading = false;
     public GameObject slider;
     public bool reloadinggun = false;
 
     void Start()
     {
 
     }
 
     void Update ()
     {
             if (Input.GetKeyDown(KeyCode.R) && bullets < 7) //if R is pressed then this happens
             {
                 deagle.animation.Play ("Reload"); //deagle is my gun, this allows me to play my reload animation if my bullets are lower than 7.
                 bullets = 7; //after reloading my bullets go back to being 7
                 reloading = false;
             }
 
             if (Input.GetButtonDown("Fire1") && reloadinggun == false) //if I press left mouse button
             {
                 deagle.animation.Play ("Fire");
                 timedown = true;
                 RaycastHit hit;
             if (time == 1 && reloading == false)
             {
                 if(Physics.Raycast (transform.position, transform.TransformDirection(Vector3.down), out hit))
                 {
                     Distance = hit.distance;
                     if (Distance < MaxDistance)
                     hit.transform.SendMessage ("ApplyDamage", Damage, SendMessageOptions.DontRequireReceiver);
                     Debug.DrawLine(transform.position, hit.point, Color.red);
                 }
             }
                 //timer starts to count down for my firerate
                 //particleClone = Instantiate(Effect, hit.point, Quaternion.LookRotation(hit.normal));
                 //Destroy(ParticleClone.gameObject, 2);
                 if (bullets == 7)
                 {
                     reloading = false;
                 }
                 if (bullets == 0) //if I run out of bullets, my gun is now out of ammo and the timer will stop otherwise it restarts and skips the first bullet in the gun.
                 {
                     reloading = false;
                     bullets = 7; //after reloading my bullets go back to being 7
                     outofammo = true; //if my bullets equal 0 then I am out of ammo triggering the next "if" statement
                     timedown = false; //timedown stops otherwise my gun will fire the first bullet automatically.
                 }
                 else 
                     outofammo = false; //if bullets does not equal 0 then I am not out of ammo
 
                 if (outofammo == true)
                 {
                     reloading = true;
                     deagle.animation.Play ("Reload"); //if the gun has 0 bullets it will automatically reload
                 }
 
             }
             
             if (timedown == true) //if my firerate is set to true do this
             {
                 time = (time - (firerate *Time.deltaTime)); //this takes away time from my timer
                 if (time <= 0) //once the timer is 0 I will be able to fire my gun again
                 {
                     bullets = bullets -1; //when my gun shoots a bullet is taken away
                     fired = true; //after I have fired play the next if statement below
                     {
                         timedown = false; //after the time is 0 then it will not continue to go down
                         time = 1f;
                         fired = false;
                     }
                 }
                     if (fired == true) //after shooting my time goes back to being 1 so it can count down again
                     time = 1;
             }
         }
 }
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
0
Best Answer

Answer by supernat · Mar 25, 2014 at 06:16 AM

If you open the animation curve editor for your reload animation, when you right click on the timeline, you can place 1 of 2 items: a keyframe, and an event trigger. Place an event trigger at the beginning and one at the end, and specify a method name to call for each. Create the two methods, one to handle the start of the reload and one to process the end. At the start of reload, you should set a flag indicating the reload is happening, and at the end, you can reset the flag. Or you can just use the end event to do something else, depends on your needs.

Comment
Add comment · Show 2 · 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 Crumpet · Mar 25, 2014 at 06:55 AM 0
Share

it seems like I can only add an event but only with "(no function selected)" as shown in the picture attachedalt text

ss (2014-03-25 at 04.53.08).png (25.3 kB)
avatar image supernat · Mar 25, 2014 at 08:41 PM 0
Share

Right, you need to create a script with a function (you can make a public void dowhatever()) and attach that script to your game object that has the animation on it. Then it will show up in the pull down here. I believe you may be able to pass a single argument, check the unity documentation if that's something you need to do. Good luck.

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

21 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 avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Renderer on object disabled after level reload 1 Answer

An OS design issue: File types associated with their appropriate programs 1 Answer

How do I access RectTransform attributes via animated script? 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