Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 Lahzar · Jul 30, 2015 at 06:04 PM · randomguncirclevectorsdistribution

Gaussian Distribution in circle on x and y axis

Hey, so I have a weapon script which is exactly like CS:GO, except for the weapon spread. As you know if you've played said shooter, you know that your bullets become less accurate the longer you hold the fire button. My script can do this, but it does so in a square using normal distribution.

I want it to instead be in a circle and using gaussian distribution! I don't know quite how gaussian distribution works yet, but Ive found a short script that does it, but I can't figure out how to do it in a circle!

Here is the necessary parts of my weapon script:

     //NOT MADE BY ME
     public static double NextGaussianDouble()
     {
         double U, u, v, S;
         
         do
         {
             u = 2.0 * Random.value - 1.0;
             v = 2.0 * Random.value - 1.0;
             S = u * u + v * v;
         }
         while (S >= 1.0);
         
         double fac = Mathf.Sqrt(-2.0f * Mathf.Log((float)S) /  (float)S);    //I just changed the doubles to floats. Is there any point in using doubles at all in this case?
         return u * fac;
     }

That was the gaussian distribution, and here is the part that makes the direction:

 w.CurSpread += 1.0f/w.MaxAmmo;
             w.Spread = w.SpreadSpeed.Evaluate(w.CurSpread)*w.MaxSpread;
             float spread = w.Spread * (float)NextGaussianDouble();
             Vector3 x = ((weapon[selected].right * w.PatternX.Evaluate(w.CurSpread) * w.Recoil.x) + (weapon[selected].right * spread));
             Vector3 y = ((weapon[selected].up * w.PatternY.Evaluate(w.CurSpread) * w.Recoil.y) + (weapon[selected].up * spread));
             Vector3 direction =  (x + y + weapon[selected].forward*100);

This version of the script nails the distribution, but it sprays it in a diagonal line! I want it to spray in a circle! Please help!

Comment
Add comment · Show 1
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 Lahzar · Jul 30, 2015 at 07:18 PM 0
Share

$$anonymous$$an, thanks! I am sure I remember your username from some other post of $$anonymous$$e? Would you $$anonymous$$d converting your comment to an answer so I may accept it?

I did want it to spread in a circle, but I decided to put that idea on a shelf for now.

1 Reply

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

Answer by _Gkxd · Jul 30, 2015 at 06:33 PM

Without looking into the details of your code, I can see that you're only calling NextGaussianDouble once. If this is the case, then you are only going to get a distribution in one dimension.

If you want a two dimensional distribution, you need to call NextGaussianDouble more than once. How you use these values is up to you, but generally you would have a "x" amount and a "y" amount.

If you use the same value for your "x" and "y" amounts, you will get a one dimensional distribution on the diagonal line y = x. I think this is what's happening, since you only seem to be calling NextGaussianDouble once.

Comment
Add comment · 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

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

22 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

Related Questions

Why does inside unit circle use the world point? 2 Answers

Random Coordinates Outside a Circle 1 Answer

Gun accuracy with Random.Range? 1 Answer

Gun Random Rotation 0 Answers

random.onUnitCircle ? 6 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