- Home /
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");
}
}
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.
Thanks :D Now it works fine! Another thing : how can I stop an animation after the $$anonymous$$ey is not pressed anymore?
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
![](https://koobas.hobune.stream/wayback/20220613200903im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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