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 /
avatar image
0
Question by connorwforman · Jan 04, 2018 at 09:23 PM · scripting problem

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

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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

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 connorwforman · Jan 04, 2018 at 10:54 PM 0
Share

@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?

avatar image sparkzbarca connorwforman · Jan 04, 2018 at 11:59 PM 0
Share

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.

avatar image
0

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.

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 MaxGuernseyIII · Jan 05, 2018 at 12:39 AM 0
Share

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.

avatar image sparkzbarca MaxGuernseyIII · Jan 05, 2018 at 01:46 AM 0
Share

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

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

124 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 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 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 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

How can I make a UI button's action happen before `Input.GetMouseButtonDown` action in another script? 0 Answers

The requested feature is not implemented. 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