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 xthunderduckx · Apr 21, 2019 at 04:01 AM · raycasteuleranglesrotationsphysics.raycast

Fixed bullet spread

I am making a skill based first person shooter, and for this reason I do not want my weapons to have deviation based on random number generation. Instead, I would like for the shotguns to spread the pellets in a specific pattern each time I fire the gun. Though I can set different values for the raycasts to use, I cannot figure out why the spread pattern changes based on where the player is looking.

alt text

As you can see, the bullet spread changes when looking around. Of course, I'd prefer that when you look down with the shotgun, the shotgun also not be turned to the right for whatever reason, as the character seems to be doing.

Using code, how would I stop the above tragedy from happening?


The script is pretty simple thus far, camera rotates with the mouse, press a button and it summons a few raycasts. These raycast's directions change based on the pre-defined angle measures I added.


The combatShotgunAngles array is three vectors, one being vector3.zero, the next being 0, 0.1, 0.1, and the last being the negative version of the latter vector. I'm looking to make that three pellet spread stay consistent no matter where the player is looking.

         if (Input.GetKey(KeyCode.Q))
         {
             for (int i = 0; i < 3; i++)
             {
                 Vector3 direction = transform.forward;
                 direction.x += combatAngles[i].z;
                 direction.y += combatAngles[i].y;
                 direction.z += combatAngles[i].z;  
                 if (Physics.Raycast(transform.position, direction, out RaycastHit testHit))
                 {
                     GameObject testObj = GameObject.CreatePrimitive(PrimitiveType.Cube);
                     testObj.transform.position = testHit.point;
                     testObj.transform.localScale *= 0.1f;
                 }
             }
         }

Edit: I have tried using transform.transformDirection, and I think it may prove helpful, though I am not sure where to place it.
There is also the possiblity I should be using quaternions for the directions and adding angles instead of a normalized array of vector's to each pellet.
My progress on reaching fixed, consistent spread, however, has been minimal.

spreads.png (30.6 kB)
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 nt314p · Apr 22, 2019 at 01:59 PM 0
Share

I believe it has something to do with world space vs local space (position vs local position).

avatar image MasterChameleonGames · Apr 22, 2019 at 09:07 PM 0
Share

Transform.forward is relative to rotation, that might be affecting it, Vector3.forward is normal z.

1 Reply

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

Answer by xthunderduckx · Apr 22, 2019 at 10:28 PM

Thanks to the help of the forum and a few members at the Newblood discord server, I was able to figure out that the vectors were missing the z value, which I didn't think was actually a necessity for this kind of thing.

I set all the z values for the spread to 1, and it worked great.

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

151 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 avatar image avatar image avatar image

Related Questions

Fake Shadow Plane: Euler Rotation Optimization 2 Answers

Raycast hits everything on the screen 2 Answers

Problem with converting RaycastHit to Vector3 2 Answers

if ( Physics.Raycast ( ray, out hit, Mathf.Infinity, LayerMask ) ) not working? 2 Answers

Set rotation values equal to 0 5 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