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 DayyanSisson · Oct 19, 2011 at 03:14 AM · arraysgunshootarraylistshot

Random Gun Jam with Array

I'm making a revolutionary war project, and the muskets back then jammed. So I can get the gun to jam with this bit of code:

 if(bulletShotCount == 30 || spitfireShotCount == 7){
 canShoot = false;
 }

bulletShotCount is a variable that increases by one every single time the shot is fired. So when the shot count equals 30, the gun "jams", and the player can no longer shoot. But I want it to jam randomly, as if to say, it can jam at 10, or at 7, or at 1, or at 2. So what I though I would do is make to variables:

 var bulletGunJam = int[];
 var spitfireGunJam = int[];

So I figured that if I made those two variables arrays, that it would choose one of the values in the array, and pick one randomly each time. It obviously didn't do that. Here's the code I used:

 if(bulletShotCount == bulletGunJam || spitfireShotCount == spitfireGunJam){
     canShoot = false;
     }

Is there another way to make it jam randomly?

Comment
Add comment · Show 2
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 syclamoth · Oct 19, 2011 at 09:33 AM 0
Share

Yeah, what @timsk said- why make it complicated? Also, the way you are doing it here, you will only jam at certain times. Are you sure that's really what you want? Using @timsk's method, you could also add something in which increases the chance of the gun jam$$anonymous$$g every time it doesn't, and then reset it when it jams.

The way I would implement that method above, btw, wouldn't be by picking a number between 1 and 10. Just use a 0-1 probability (since that's how probability is calculated anyway) and compare it with Random.value.

avatar image timsk · Oct 19, 2011 at 09:36 AM 0
Share

Well, i changed my comment to an answer and added an example. Would be interested to see how you would have done it syclamoth.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by timsk · Oct 19, 2011 at 09:32 AM

why not just, everytime you take a shot, pick a number between 1 and 10. if the number is higher than 5 jam the gun, if not, allow the gun to fire.

example:

 var shotJamChance : int;
 
 function JamCheck()
 {
     shotJamChance = Random.range(1,10)
     if(shotJamChance >=5)
     {
         Shoot();
     }
     else
     {
         JamGun();
     }
 
 }

This would give you a 50% chance to jam, so just weight it whichever way you want, for more randomness, increase the number (so 1-50 would be more random than 1-10)

Comment
Add comment · Show 4 · 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 syclamoth · Oct 19, 2011 at 10:15 AM 0
Share

Well, my way is basically the exact same, only with numbers between 0 and 1, ins$$anonymous$$d of 1 and 10.

avatar image DayyanSisson · Oct 19, 2011 at 08:21 PM 0
Share

It works, but the only problem is, in the inspector, the shotJamChance integer changes several times a second. I only want it to change every time the player shoots. How should I do that?

avatar image timsk · Oct 20, 2011 at 09:37 AM 0
Share

Sounds like your running JamCheck() inside Update(). You need to do something like this:

 function ShootGun()
 {
     //gun shoot code here
     JamCheck()
 }

Then run ShootGun() inside On$$anonymous$$ouseDown(1) or something similar. This way, you only check for a jam when the player shoots.

avatar image DayyanSisson · Oct 21, 2011 at 12:31 AM 0
Share

Thanks it works!

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Shoot only once when clicked 1 Answer

Best way to play reload animations for a gun?? 1 Answer

How to Clone Array1 into Array2 in Unity? Using JavaScript. 0 Answers

Bullets will not fire forwards 1 Answer

arraylist stroing/passing values? 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