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 ma22be61 · Jun 11, 2015 at 09:49 PM · animationfunctiononce

Finish the function withour repeating after clicking more than 1 time

Ok guys I can't really get it : how can I finish a full function (created by me)after clicking more than one time with mouse? I mean : everytime i press Mouse0 more than 1 time the function just repeats a lot of times! I want that the first time you press Mouse0 the function just finish (you can't execute other functions while 1 is playing) no matter how many times you press Mouse0! That's the code :

 var pumplist : AnimationClip[];
 var target : GameObject;
 private var pumpnumber : int = 0;
 private var drawed : boolean = true;
 private var playerview : boolean = true;
 
 private var zoomIn : boolean = true;
 private var zoomStart : boolean;
 private var unZoom : boolean;
 

function Update()

 {
 if (zoomStart)
 {
 GetComponent.<Camera>().fieldOfView = GetComponent.<Camera>().fieldOfView-1;
 if (GetComponent.<Camera>().fieldOfView<31) zoomStart = false;
 }
 if (unZoom)
 {
 GetComponent.<Camera>().fieldOfView = GetComponent.<Camera>().fieldOfView+1;
 if (GetComponent.<Camera>().fieldOfView>49.1) unZoom = false;
 }
 
 if(Input.GetKey(KeyCode.R))                //Reloading by pressing R : that's the
                                                       problem!If i press R more than 1 time animation repeats and don't let the other one finish with a really ugly effect . The same for shooting!
 {
 reload();
 }
 
 if(Input.GetKey(KeyCode.Mouse0))        //Shooting with mouse
 {
 fire();
 }
 
 }
 
 //Shooting function
 function fire () {    
 if (Input.GetKeyDown(KeyCode.Mouse0))
 target.GetComponent.<Animation>().Play("fire");
 yield WaitForSeconds(0.3);
 target.GetComponent.<Animation>().Play(pumplist[pumpnumber].name);
 pumpnumber ++;
 if (pumpnumber == 2) pumpnumber=0;
 yield WaitForSeconds(0.8);
 target.GetComponent.<Animation>().Play("idle");
 }
 
 //Reload function
 function reload () {
 if (Input.GetKey(KeyCode.R))
 target.GetComponent.<Animation>().Play("reloadStart");
 target.GetComponent.<Animation>().CrossFade("reloadCycle",0.65);
 yield WaitForSeconds(3.8);
 {
 target.GetComponent.<Animation>().Play("reloadStop");
 }
 }
   
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
1
Best Answer

Answer by Gardes · Jun 12, 2015 at 03:19 AM

I suggest create a new bool (finished in the example) and do it like this:

     void Update () {
     
         if (Input.GetKey (KeyCode.Mouse0) && finished) {
             finished = false;
             FunctionYouWantToCall ();
         }
     }
 
     void FunctionYouWantToCall() {
         // your Code here
         finished = true;
     }

Now the function can only get called if its already finished. The cool thing is, if you implement finished in reloading and shooting, no one can override the other or themself.

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 ma22be61 · Jun 12, 2015 at 11:51 AM 0
Share

Thanks :D Now it works fine! Another thing : how can I stop an animation after the $$anonymous$$ey is not pressed anymore?

avatar image Gardes · Jun 12, 2015 at 02:41 PM 0
Share

Im glad :)

Hmm, this would be the part where your code gettin weird. You realy should get familiar with the animator. $$anonymous$$aybe you could do it like

          if (!Input.Get$$anonymous$$ey ($$anonymous$$eyCode.$$anonymous$$ouse0) && finished) {
              //idleAnimation
          }

Just a bit pseudo, dunno if it works in your scene.. I just can say it again, watch some tutorials about the animator. You will love it, once you find out how easy it works :p

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

AI function/coroutine animation issues 2 Answers

Can I make animations snap to a frame? 1 Answer

Animation keeps looping even on WrapMode.Once; 1 Answer

Playing an Animation only once Unity 4 0 Answers

Call a function dependent on frame of animation? 2 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