• 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
Question by Bonkahe · Nov 18, 2013 at 03:33 PM · raycastgunaiming

Raycast Targeting Reticle.

Basically I have some guns, you can switch between them, and shoot them, and I have them creating a raycast down the barrel so that when its done it will render a reticle at 5 meters out or on the object in front of the gun, (It's a third person shooter, thus a reticle in the middle of the screen simply will not do.) All in all it's going quite well, but I hit a snag, I have a model (just a simple ring) and I want to have it be spawned wherever the ray ends, be that an object or just floating in the air at the end of the ray.

the script for this part of the guns pretty simple:

     void Reticle ()
     {
         Vector3 fwd = BulletSpawn.transform.TransformDirection(Vector3.up);
         if (Physics.Raycast(transform.position, fwd, 5))
         {
             print("There is something in front of the object!");
         }
         Debug.DrawRay(BulletSpawn.transform.position, fwd, Color.green);
     }

If you want the hole script I can pull that from my files. Any thoughts guys?

Comment

People who like this

0 Show 0
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
Best Answer

Answer by Xelareip · Nov 18, 2013 at 04:37 PM

 RaycastHit hitInfo = new RaycastHit();
 
 if (Physics.Raycast(transform.position, fwd, 5, hitInfo))
 {
     Vector3 impactPoint = hitInfo.point;
     // Create your ring at impactPoint
 }


The RaycastHit object passed in parameter should give you all the info you need, especially the position where the ray hit something. You can then move your ring at that position.

Comment
Lysander

People who like this

1 Show 8 · 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 Bonkahe · Nov 19, 2013 at 01:56 AM 0
Share

I'll implement this and see how it works, thanks man, if it works I'll thumbs up your answer definently.

avatar image robertbu · Nov 19, 2013 at 02:27 AM 0
Share

The above code will give you the position of the hit, but if it does not hit something, you need to add code to calculate the position. Maybe something like:

 RaycastHit hitInfo = new RaycastHit();
 Vector3 showPos;
  
 if (Physics.Raycast(transform.position, fwd, 5, hitInfo)) {
     showPos = hitInfo.point;
 }
 else {
     showPos = transform.position + fwd * 5.0f;
 }

// Place your ring at showPos;

avatar image Bonkahe · Nov 19, 2013 at 02:51 AM 0
Share
     void Reticle ()
     {
         Vector3 fwd = BulletSpawn.transform.TransformDirection(Vector3.up);
         RaycastHit hitInfo = new RaycastHit();
         Vector3 showPos;
          
         if (Physics.Raycast(transform.position, fwd, 5, hitInfo)) 
         {
             showPos = hitInfo.point;
         }
         else 
         {
                showPos = transform.position + fwd * 5.0f;
         }
         Instantiate(Reticle, showPos.position);
         Debug.DrawRay(BulletSpawn.transform.position, fwd, Color.green);
     }

Derp, I am failing, I need sleep.

Edit: I didn't really explain, I can't figure out how to pull transform from showpos >.< I will probably be able to figure it out in the morning.

avatar image Bonkahe · Nov 21, 2013 at 03:09 AM 0
Share

Ok now I'm running into these errors.

 Assets/Scripts/Weapon/PistolController.cs(42,25): error CS1502: The best overloaded method match for `UnityEngine.Physics.Raycast(UnityEngine.Vector3, UnityEngine.Vector3, float, int)' has some invalid arguments


 Assets/Scripts/Weapon/PistolController.cs(42,25): error CS1503: Argument `#4' cannot convert `UnityEngine.RaycastHit' expression to type `int'

The current script is:

     void Reticle ()
     {
         Vector3 fwd = BulletSpawn.transform.TransformDirection(Vector3.up);
         RaycastHit hitInfo = new RaycastHit();
         Vector3 showPos;
         if (Physics.Raycast(transform.position, fwd, 5, hitInfo)) 
         {
             showPos = hitInfo.point;
         }
         else 
         {
                showPos = transform.position + fwd * 5.0f;
         }
         reticle.transform.position = showPos;
         Debug.DrawRay(BulletSpawn.transform.position, fwd, Color.green);
     }
avatar image Xelareip · Nov 21, 2013 at 09:39 AM 0
Share

Hmm, that's my bad, the order of the parameters is wrong :

Physics.Raycast(transform.position, fwd, hitInfo, 5.0f)

Also, since the distance parameter is a float, 5.0f is better than 5.

  • http://docs.unity3d.com/Documentation/ScriptReference/Physics.Raycast.html

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

18 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

Related Questions

Why are my guns shooting lower when my character moves forward? 1 Answer

How can I decrease accuracy when the gun is fired? 2 Answers

stop gun from rotating 1 Answer

How to Implement First Shot Accuracy? 2 Answers

Aiming Down the Sights Script (HELP) 0 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