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
5
Question by LegionIsTaken · Jun 30, 2011 at 08:25 PM · raycastguitextaccuracy

Gun accuracy

Hello everyone

I am making a FPS prototype and it is going very well, but I can´t rap my brain around gun accuracy and showing a moving crosshair on a Guitext. My gun uses raycast to fire, but it is shooting in a strait line using transform.forward. I all ready looked at DastardlyBanana´s FPS package, but lets face it, it is just a little too complicated for me.

So can you give me some leads on how to make a spray effect using raycast, and a moving crosshair showing the guns accuracy on a Guitext?

Thank you.

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

2 Replies

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

Answer by PrimeDerektive · Jun 30, 2011 at 08:32 PM

Spread on a raycast is pretty simple. Just add a random factor to the x y and z of your direction vector before you pass it to the raycast. Something like this:

 //declare a float value to control the spread factor. .02 is a good default
 public var spreadFactor : float = 0.02;


then when you're doing the raycast, instead of passing it transform.forward, store it in a variable before hand and add the spread.

 var direction : Vector3 = transform.forward;
 direction.x += Random.Range(-spreadFactor, spreadFactor);
 direction.y += Random.Range(-spreadFactor, spreadFactor);
 direction.z += Random.Range(-spreadFactor, spreadFactor);
 
 if (Physics.Raycast (transform.position, direction, hit, distance)){ 
Comment
Add comment · Show 2 · 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 LegionIsTaken · Jun 30, 2011 at 09:05 PM 0
Share

That is awesome and it works. I can´t believe it is "that" simple. I can´t and will not ask for more, but in any case do you know how to show it on a GuiText(crosshair)? :)

avatar image Zarenityx · Jun 12, 2013 at 09:05 PM 0
Share

That might be odd to view, because if you were constantly calculating your angle and then displaying it on the crosshair, you would have a barely visible GUI that constantly flickers around the center of the screen with no precision or order. It might be better to just add a line or trail(depending on how you are making these bullets), and a bullet hole where it hits. Besides, the idea of gun accuracy is to make a gun a bit unreliable, so that better accuracy is, well, better. If you already know where your bullet is going to go, a shotgun would have effectively the same accuracy at a distance a sniper rifle, because the player can just adjust.

avatar image
0

Answer by JosephLim · May 10, 2018 at 06:36 AM

Hi can i ask for the C# part for this?

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 Aleksa_Djordjic · Oct 10, 2018 at 06:20 PM 1
Share

It is similar to the JS version , just replace how variables are declared :

 public float spreadFactor = 0.02f;
 
 Vector3 direction = transform.forward;
 
 direction.x += Random.Range(-spreadFactor, spreadFactor);
 direction.y += Random.Range(-spreadFactor, spreadFactor);
 direction.z += Random.Range(-spreadFactor, spreadFactor);
  
 if (Physics.Raycast (transform.position, direction, out hit, range, canHitLayer$$anonymous$$ask))
 {
     //Shooting Code
 }

*Note : I havent tested this, im just writing it how i think it should be, and i might be late to answer this ..... so xD

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

RaycastHit info works using print() but not GUIText? 1 Answer

How to implement gun accuracy/spread with raycast 1 Answer

Accuracy not working properly 1 Answer

How to give your enemy gun inaccuracy 1 Answer

More precise Vector3 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