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 /
avatar image
0
Question by impurekind · Sep 20, 2018 at 12:16 PM · raycastpositiondirectionanglerandom.range

How to shoot raycast with slight random direction?

I want to shoot a raycast to check where my gun has fired, but with the shotgun I'm going to want to randomize the precise direction of the ray slightly with each shot.

How would I do this?

Currently I use if (Physics.Raycast(shotOrigin.position, shotOrigin.forward, out hit, 1000, layerMask)) to shoot a ray straight out (forward) from the barrel of the gun with each shot, so I need to find a way to vary that "forward" angle up a little bit.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Casiell · Sep 20, 2018 at 12:54 PM

You might want to use Quaternion.AngleAxis. Create a random float for your angle and multiply shotOrigin.Forward by the value you will get from AngleAxis value

Comment
Add comment · Show 5 · 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 impurekind · Sep 20, 2018 at 01:02 PM 0
Share

Can you show me how this would look in code with an example, because I'm not sure what you mean (as in how to write it correctly)?

Here's my code as it exists right now:

 private void RaycastGun()
     {
         int numberOfBullets = 1;
 
        if (WeaponSelectionController.weaponNumberRightHand == 2) // The Shotgun
         {
             numberOfBullets = 15;
         }
 
         int layer$$anonymous$$ask = ((1 << 10) | (1 << 11) | (1 << 13));
 
         layer$$anonymous$$ask = ~layer$$anonymous$$ask;
 
         RaycastHit hit;
 
         for (int i = 0; i < numberOfBullets; i++)
         {
             if (Physics.Raycast(shotOrigin.position, shotOrigin.forward, out hit, 1000, layer$$anonymous$$ask))
             {
                 GameObject theObject = hit.collider.gameObject;
 
                 if (hit.collider.gameObject.CompareTag("Enemy"))
                 {
                     // Do stuff
                 }
             }
         }
     }
avatar image Casiell impurekind · Sep 20, 2018 at 01:29 PM 0
Share

Something like this:

 float randomAngle = new System.Random($$anonymous$$Angle, maxAngle);
 Vector3 axis = new Vector3(1, 1, 0);
 var rotation = Quaternion.AngleAxis(randomAngle, axis);
 
 if (Physics.Raycast(shotOrigin.position, rotation * shotOrigin.forward, out hit, 1000, layer$$anonymous$$ask))

I'm only not sure if I'm using the correct axis, so if you get weird results you might want to tinker with that first.

avatar image impurekind Casiell · Sep 20, 2018 at 01:45 PM 0
Share

Cool.

I'll give it a go and see what happens. . . .

Show more comments

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

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

Get angle from Object to nearest point of other Object 0 Answers

how to find direction between two points 2 Answers

How to get offset postition after rotation ? (Spent hours figuring it out) 3 Answers

How to add slight angle/rotation variation to transform.forward? 5 Answers

Raycast across angle? 3 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