You know the drill: my shoot script isn't working (again.)
Alrighty, welcome back to another episode of "My Shoot Script Won't Work". The problem is this: when I hold down the LMB, and isShooting = true, the bullet only fires once, like its on semi-auto. And, when I add the "break" function, it gives me this error: "No enclosing loop out of which to break or continue. How would I fix both of these problems? The script file is too large to post with no error, so I attached the file to github, and there is no download required.
https://github.com/connorwforman/shootscript/blob/master/.gitignore/.gitignore
Answer by sparkzbarca · Jan 04, 2018 at 09:39 PM
if (Input.GetButtonDown ("Fire1")) {
needs to be if get button("Fire1")
see this https://docs.unity3d.com/ScriptReference/Input.GetButtonDown.html ...It will not return true until the user has released the key and pressed it again.
So even though your holding the button down, after the first frame if(getbuttonDown..) returns false after the frame in which you clicked and it doesn't fire again because of that. Basically getbutton down is expressly for one click because of course people can't act within a single frame often so if you want for example a single firing rifle you dont' want to count 10 clicks cause it took them 10 frames to press and release when your running at 200fps
getbutton gets the current state and is for in this case automatic fire
get button down is a single click
get button up is the same way.
single release, get button up for example isn't true the whole time your not clicking the mouse
@sparkzbarca, so I only need to change the if (Input.GetButtonDown("Fire1"))? Or do I need to change the second one? (If so, how?) And how do I fix the "break;" problem?
To answer your break question, break is used to end loops, either a for or while statement. Your not in a loop, your in a function.
In this case, you simply want to stop the function early (now)
Because of that.
replace
break;
with
return;
and leave the function.
Answer by MaxGuernseyIII · Jan 04, 2018 at 11:10 PM
I would change line 93, in your coroutine, to
while (isShooting == true) {
That way, your coroutine will keep firing, animating, and waiting until the shooting stops.
Once you switch it to a while loop, the break should start to compile correctly but, if what you really want is to break out of the shooting state entirely, you can use yield break.
IEnumerator BustOutOfLoop() {
while (Shooting()) {
if (!SufficientAmmo())
yield break;
if (Hit())
DoHit();
ConsumeAmmo();
StartAnimationsAndSound();
yield return new WaitForSeconds(shotDelay);
}
}
I abstracted away the details so we could focus on the overall algorithm. In general, this will help you get what you want more readily. One thing you can do is start with the abstract algorithm and fill in the blanks later. $$anonymous$$y father called this "top-down" program$$anonymous$$g. $$anonymous$$y generation calls it "program$$anonymous$$g by intention". I have no idea what the kids are calling it, these days. ;)
Anyway, part of the value of making it a coroutine is that you can have the while look you showed in that previous question and pretty much write it exactly the way you wanted to, just using yields to create delays and give other threads a chance.
haven't really had like formal program$$anonymous$$g training, i kind of do the "program$$anonymous$$g by intention" thing sometimes.
Like I might make a blank function for later.
But i never did it in the complete way you've shown here where the moment you come across a kind of function or value you just write in a blank one and keep writing and later fill in the blanks.
Seeing that now was very helpful lol, i'll be using it a lot so thanks
Your answer
Follow this Question
Related Questions
How to create a plot system 0 Answers
How do I ignore input when player object has reached a certain point? 1 Answer
How to tilt NavMeshAgent? 0 Answers