Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
This question was closed Nov 28, 2015 at 10:33 PM by Vylax for the following reason:

I finded a solution

avatar image
1
Question by Vylax · Nov 08, 2015 at 12:00 PM · animationfps controllerwaitreloading

Wait for animation end before continue script JS

Hi, first I want to say you I searched in all same topic and question and I didn't find an answer so please don't give me this kind of answer :

http://lmgtfy.com/?q=unity+3d+wait+for+animation+finish

or

Hey, here's a lot of same question : http://answers.unity3d.com/questions/134960/finish-animation-before-continuing-script.html http://answers.unity3d.com/questions/336333/wait-for-animation-to-finish.html http://answers.unity3d.com/questions/964586/waiting-for-animation-to-finish-playing.html

...

So, I'm making an FPS, and I want to my player can't shot or anything else (exept walking) if he is reloading. My script :

 #pragma strict
 
 ///////////////////////////////////////////////////////////////////VARIABLES///////////////////////////////////////////////////////////////////////
 
 var bulletCasing : Rigidbody;
 var ejectSpeed : int = 100;
 var fireRate : float = 0.5;
 private var nextFire : float = 0.0;
 private var fullAuto = false;
 
 var clip : int = 30;
 var maxclip : int = 30;
 var reserve : int = 300;
 var minreserve : int = 0;
 
 var shotsound : AudioClip;
 var reloadsound : AudioClip;
 
 var MunMax : boolean = true;
 var reloadsoundplay : boolean = false;
 
 var eject : Transform;
 
 var anim : Animator;
 var isShooting : boolean = false;
 var isReloading : boolean = false;
 
 ///////////////////////////////////////////////////////////////////FONCTION UPDATE////////////////////////////////////////////////////////////////
 
 
 function Start () {
 anim = GetComponent(Animator);
 }
 
 function Update () {
     
 // SECTION DE TIR
 if(Input.GetButton("Fire1") && Time.time > nextFire){
 if(clip >= 1){
 nextFire = Time.time + fireRate;
 
 var bullet : Rigidbody;
 
 bullet = Instantiate(bulletCasing, eject.position, eject.rotation);
 clip -= 1;
 GetComponent.<AudioSource>().PlayOneShot(shotsound);
 animation.Play("shot");
 bullet.velocity = eject.TransformDirection(Vector3.left * ejectSpeed);
 }
 }
 
 //SECTION DE CADENCE DE TIR
 if(Input.GetKeyDown("v")){
 fullAuto = !fullAuto;
 }
 
 //SECTION DE RECHARGE
 if(Input.GetKeyDown("r")){
 
 if(reloadsoundplay == true){
 GetComponent.<AudioSource>().PlayOneShot(reloadsound);
 animation.Play("reload");
 
 }
 
 if(reserve > 30){
 RemoveReserve();
 clip += maxclip - clip;
 }
 
 if(reserve < 30){
 clip += reserve;
 RemoveReserve();
 }
 
 }
 
 //CHANGEMENT DE CADENCE DE TIR
 if(fullAuto == true){
 fireRate = 0.1;
 }else{
 fireRate = 0.5;
 }
 
 //BLOQUER LA RESERVE A 0
 if(reserve <= 0){
 reserve = 0;
 }
 
 //JOUER LE SON UNIQUEMENT QUAND LE CHARGEUR ACTUEL N'EST PAS COMPLET
 if(clip == maxclip){
 reloadsoundplay = false;
 }
 
 if(clip < maxclip){
 reloadsoundplay = true;
 }
 
 }
 
 ///////////////////////////////////////////////////////////////////FUNCTION ON GUI///////////////////////////////////////////////////////////////////////
 
 function OnGUI(){
 GUI.Box(Rect(10,10,130,25), clip+ " / " +reserve);
 }
 ///////////////////////////////////////////////////////////////////FUNCTION REMOVE RESERVE///////////////////////////////////////////////////////////////
 
 function RemoveReserve(){
 reserve -= maxclip - clip;
 }
 
 
 
Comment
Add comment · Show 1
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 Alakanu · Jun 29, 2016 at 04:13 PM 0
Share

So you found a solution, good for you. Now can you share that solution, so that maybe the community can use the information?

1 Reply

  • Sort: 
avatar image
0

Answer by Asgardr · Nov 08, 2015 at 12:25 PM

You could use a co-routine that yields for the length of the animation. An example is in the docs:

http://docs.unity3d.com/ScriptReference/AnimationClip-length.html

 var anim: Animation;
 
 function Start() {
     anim = GetComponent.<Animation>();
     
     anim.Play();
     
     // Wait for the animation to finish.
     yield WaitForSeconds(anim.clip.length);
 }

You could then do something like setting a bool isAllowedToFire. Set it to false if the animation is playing, and true if the animation stopped. Something like the following:

 var anim: Animation;
 var isAllowedToFire: boolean;
 
 function Fire() {
     isAllowedToFire = false;

     anim = GetComponent.<Animation>();
     
     anim.Play();
     
     // Wait for the animation to finish.
     yield WaitForSeconds(anim.clip.length);

     isAllowedToFire = true;
 }

Comment
Add comment · Show 1 · 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 Vylax · Nov 08, 2015 at 03:18 PM 0
Share

I don't really understand sorry, I always have problem with couroutine I don't understnd how it work.

Follow this Question

Answers Answers and Comments

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

Related Questions

How do I add running and animations to my FPS controller script? 0 Answers

Full body gun switcher for animations? 1 Answer

Rotate the player with animation 2 Answers

FPSController won't move with platform 0 Answers

Animation Won't Play Before My Time Freeze. 0 Answers


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