• 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 melonman · Sep 02, 2011 at 02:01 PM · raycastingdistance

distance between object and raycast hit.point

Im new to Raycasting and have manageed to write some code that allows the casting to emit and tell me what gameObject it's pointing at which is great.

I know want to be able to specify at what distance it tells me what game object im pointing at so.........enter RaycastHit.distance.

I looked on the Unity website script reference and there is barely any information on it and how to use it. I have tried to incorporate it in script in a number of ways to get the distance between the camera that is emitting the ray and the rays hit point but with no luck.

Does anyone know how I would use this to get the distance between my camera and any varying hit point?

here are some of the attempts that I have played around with but haven't succeeded with

activation_distance = (RaycastHit.point - Camera.main.transform.position)

activation_distance = Vector3.Distance(Camera.main.transform.position, RaycastHit.point);

RaycastHit.distance = Camera.main.transform.position, hit.point;

RaycastHit.distance(Camera.main.transform.position, hit.point);

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
3

Answer by aldonaletto · Sep 02, 2011 at 03:20 PM

You're confusing two different things. In Physics.Raycast, you can use the optional parameter distance to specify the ray detection range. If you don't use it, the ray will go from the starting point to infinite. Usually you only use this parameter if you want to limit the Raycast detection range - if you want to check if something is at up to 10m, for instance.
Upon return, if Raycast returns true, the RaycastHit hitInfo structure is filled with hit data, what includes distance - the distance from the ray starting point to the hit point, in this case.
Take a look at the last example in Physics.Raycast. You can also see two different usages in the following:

// create a ray starting at the camera and passing through the mouse pointer
var ray: Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
var hit: RaycastHit;
// do a infinite Raycast from the camera
if (Physics.Raycast(ray, hit)) print("Hit something at "+hit.distance+" meters");
// do a Raycast up to 50 meters from the starting point
if (Physics.Raycast(ray, hit, 50)) print("Hit "+hit.transform.name);
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 melonman · Sep 03, 2011 at 09:47 PM 0
Share

Thanks aldonaletto I getcha now.

I had another go and got some help as well and used the hit.distance method to get the result I wanted but it's much better knowing what the hell does what as far as Physics.Raycast and RaycastHit.hitInfo are concerned. Those will be useful for this step and the next.

Thanks again.

avatar image wechat_os_Qy00fbKpXihT19QGbt9fpA9UM · Feb 06, 2021 at 03:33 AM 0
Share

might need an "out" before "hit"

 if (Physics.Raycast(ray, out hit)) print("Hit something at "+hit.distance+" meters");

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How to perform some code if you are close to an object 2 Answers

Alternative to remainingDistance (Entire Path Calculation)? 0 Answers

Raycast stop at distance 3 Answers

How to calculate direction between 2 objects 2 Answers

How to control multiple gradients 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