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 ExTheSea · Mar 13, 2013 at 01:00 PM · randomrangeaccuracy

two Random.Range returning similar values

Hey guys,

In my MachineGun Script for an Ego-Shooter i'm currently working on uses a Random number to calculate the decreased accuracy when shooting from the hip. I try to achieve this with the use of Random.Range/Random.value like this:

 if(!scoping)
 {
     var lessaccuracyx = (Random.Range(-0.5, 0.5))*(spread.spread/220);
     var lessaccuracyy = (Random.Range(-0.5, 0.5))*(spread.spread/220);
 }
 ...
     if (Physics.Raycast (cam.position+cam.transform.forward.normalized*1, cam.forward+ Vector3(lessaccuracyx, lessaccuracyy, 0), hit, range, layerMask)) {
 ...

spread increases when you rapid fire and for that causes the gun to be less accurate over time.

When I fire the gun noscoped at sometimes it seams the work as the bullet holes are scattered like they are supposed to be but after a while the seem to align in a line either completly horizontal, completly vertical or from the top left to the bottom right.

From this behaviour i would assume that the Ramdom value produce similar values.

I'm a bit lost here because sometimes it seems to work fine but then the lines appear.

Can someone please help me? If you need more information please let me know.

Well i looked over my script again and came to the conclusion that i made it overly complex so I changed it to this:

     var spreadfloat : float = ((spread.spread/100)/2);
     lessaccuracyx = Random.Range(-spreadfloat, spreadfloat);
     lessaccuracyy = Random.Range(-spreadfloat, spreadfloat);
 

spreadfloat is somewhere around 0.25... Still the bulletHole seem to align roughly around a line from the top left to the bottom right.

Example:

alt text

picaccuracy.jpg (6.4 kB)
Comment
Add comment · Show 7
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 amphoterik · Mar 13, 2013 at 01:52 PM 0
Share

This probably isn't it, but it is a good idea to put an 'f' at the end of your float values to enforce the type: Random.Range(-0.5f, 0.5f)

avatar image AlucardJay · Mar 13, 2013 at 02:00 PM 1
Share

What kind of value is spread.spread/220 returning? If it is a value less than one, then yes you are going to generate some very low numbers. If you are unsure of the values returned by Random, isolate them and exa$$anonymous$$e them in the inspector or in a console debug :

 if(!scoping)
 {
     var rndX : float = Random.Range(-0.5, 0.5);
     var rndY : float = Random.Range(-0.5, 0.5);
     Debug.Log( "rndX = " + rndX  + " : rndY = " + rndY  );
     var lessaccuracyx : float = rndX *(spread.spread/220);
     var lessaccuracy : float = rndY *(spread.spread/220);
 }
avatar image ExTheSea · Mar 13, 2013 at 03:05 PM 0
Share

Thank you for your reply. Yes the values I'm working with are lower than 1. They range around -0.25 to +0.25 .

avatar image amphoterik · Mar 13, 2013 at 03:28 PM 0
Share

What are sample values for spread.spread?

avatar image amphoterik · Mar 13, 2013 at 03:30 PM 0
Share

I did a test just guessing at the spread value and get some consistently spread values. No lines for me. $$anonymous$$aybe the problem is in your raycasting? I don't do much raycasting so I am unfamiliar with the syntax.

Show more comments

1 Reply

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

Answer by Owen-Reynolds · Mar 13, 2013 at 04:26 PM

The raycast is using x & y along a global axis. It should be a local axis. When you face due North or South, it should work mostly fine (since your right is also +x,) but as you turn East/West, it will get strange (since +x is now away/towards you.) To get local:

 startPos = cam.position + cam.forward; // same as before
 rayDir = cam.forward + cam.right*lessAccurX + cam.up*lessAccurY; // <-- local axis
 Phy.Raycast( startPos, rayDir, ....);

NOTES: your code normalizes cam.forward. No need, since it already is. If you need to push it forwards more, cam.forward*5 works.

For aiming, can also use cam.transformDirection(lessAccurX, lessAcurY, 0); as a replacement for cam.right and cam.up. Both ways say to take lessAcurX/Y and twist it to the way you are facing.

Comment
Add comment · Show 1 · 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 ExTheSea · Mar 13, 2013 at 05:19 PM 0
Share

Wow that works like a charm. Thank you very much.

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

14 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

Related Questions

Random object, Array index out of range 0 Answers

How can I Instantiate and do it in a random spot in a random spot? 2 Answers

How can I generate random number except one in javascript 2 Answers

Walk to random waypoint 1 Answer

How To Random Range a Float? 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