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
1
Question by FuRy · Mar 03, 2014 at 08:52 PM · raycastshootingbulletcrosshair

Random Raycast inside crosshairs

I am trying to get my raycast(bullets) to shoot inside the spread of the crosshairs. The crosshair is made up for 4 GUI.Box's that spread based on a number of things.

Screenshot of what I'm trying to achieve. I would want the raycast to shoot randomly in the green area which is variable based on the crosshairs spreading.

alt text

Spread class:

     public class spreading
     {
         public float sSpread = 20;
         public float maxSpread = 40;
         public float minSpread = 2;
         public float spreadPerSecond = 20;
         public float spreadWhileShooting = 50;
         public float decreasePerSecond = 25;
         public float maxForwardSpread = 25;
         public float strafeDecrease = 45;
         public float moveForwardSpreadPerSecond = 45;
     }

Crosshair drawing:

         GUI.Box(new Rect(centerPoint.x - (width / 2), centerPoint.y - (newHeight + spread.sSpread), width, newHeight), GUIContent.none, lineStyle);
         GUI.Box(new Rect(centerPoint.x - (width / 2), (centerPoint.y + spread.sSpread), width, newHeight), GUIContent.none, lineStyle);
         GUI.Box(new Rect((centerPoint.x + spread.sSpread), (centerPoint.y - (width / 2)), newHeight, width), GUIContent.none, lineStyle);
         GUI.Box(new Rect(centerPoint.x - (newHeight + spread.sSpread), (centerPoint.y - (width / 2)), newHeight, width), GUIContent.none, lineStyle);

Example shooting that makes crosshair spread

 if (Input.GetKey(KeyCode.Mouse0))
         {
             spread.sSpread += spread.spreadWhileShooting * Time.deltaTime;
             Fire();
         }


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
1
Best Answer

Answer by robertbu · Mar 03, 2014 at 09:02 PM

I'm not sure how the green area is measured. Let's assume that the diameter of the green circle is equal to spread.sSpread. You can get a random point inside that circle by:

 Vector3 pos = centerPoint + Random.insideUnitCircle * spread.sSpread / 2.0f;  

I don't know how your firing works. If it is Raycasting, then 'pos' would be the position you use to calculate the ray for casting:

 Ray ray = Camera.main.ScreenPointToRay(pos);
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 FuRy · Mar 03, 2014 at 09:08 PM 0
Share

Sorry I should have made it clear. The green circle is what I added in using photoshop. That is the area I'm not sure how to calculate.

avatar image FuRy · Mar 03, 2014 at 09:18 PM 0
Share

Actually I implemented your code and it looks about right thanks!

avatar image robertbu · Mar 03, 2014 at 09:40 PM 0
Share

You can adjust the size of the circle by dividing by something other than 2.0 or alternately multiplying by something other than 0.5.

avatar image FuRy · Mar 03, 2014 at 11:03 PM 0
Share

Thanks robert I actually played around with dividing it by something else

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

20 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

Related Questions

shooting problem with raycasting 1 Answer

How should I handle masses of bullets? 1 Answer

Shooting style like enter the dungeon 0 Answers

Best way to shoot physical bullets? 2 Answers

Raycasr in my fps? 1 Answer


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