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
0
Question by YoungDeveloper · Aug 09, 2013 at 01:26 PM · cameravector3distanceraycasthit

Vector3.Distance between camera and raycasthit

Hi, I'm trying to calculate the distance between player camera transform and raycasthit transform, everything seems okay but i get loop of infinite errors.

 float distance = Vector3.Distance (hit.collider.transform.position, Camera.main.transform.position);

I tested the code like this

 float distance = Vector3.Distance (hit.collider.transform.position, hit.collider.transform.position);

(in case i was wrong with ray hit object) but it works fine, so the problem must be in finding camera transform.

Error: NullReferenceException UnityEngine.Component.get_transform () (at C:/BuildAgent/work/cac08d8a5e25d4cb/Runtime/ExportGenerated/Editor/UnityEngineComponent.cs:21) Player.Update () (at Assets/Scripts/Player.cs:77)

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

Answer by meat5000 · Aug 09, 2013 at 01:29 PM

Hello

http://docs.unity3d.com/Documentation/ScriptReference/RaycastHit-distance.html

I assume you are performing screentouch or something like that?

RaycastHit contains the information already. It can return the distance from Ray Origin to Hit.

Also, check the name of your camera is correct for the call; NullReferenceException says it cant find something that has been called.

Your Raycast contains a distance 5.0f :

 if(Physics.Raycast(look, out hit, 5.0F))

This casts a ray over a distance of 5 world units. Unless the object is within 5 units it will not hit. Remove the 5.0f to cast an infinite ray then retrieve the distance from hit.distance.

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

Answer by YoungDeveloper · Aug 09, 2013 at 01:48 PM

Hi there, thank you for your answer, it's still kinda strange i can't recalculate it using Vector3.Distance.

Works like this (posting just for the record).

 float distance = hit.distance;

Thanks again.

Comment
Add comment · Show 6 · 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 meat5000 ♦ · Aug 09, 2013 at 02:09 PM 0
Share

Can you post your script? Particularly the perfor$$anonymous$$g of the raycast. In hit.distance I'm assu$$anonymous$$g:

hit : RaycastHit; ?

This will return false unless a Collider is hit by the ray.

Look at this thread: http://answers.unity3d.com/questions/162753/raycasthitdistance.html

It might clear some things up on the usage of Physics.Raycast and when the RaycastHit info is actually populated with info.

avatar image YoungDeveloper · Aug 09, 2013 at 03:04 PM 1
Share

Hey, i think the code is alright because it checks if collider is hit.

 private RaycastHit hit;
 
 //in update
 Ray look = camera.ScreenPointToRay(Input.mousePosition);
 
 if(Physics.Raycast(look, out hit, 5.0F)){        
 if(hit.collider.gameObject.tag == "Pickable"){
 ....
avatar image YoungDeveloper · Aug 09, 2013 at 03:24 PM 0
Share

Same, as soon as i hit the collider it gives same error infinite times. I guess you can't calculate the distance using vector3, if the calculation is already stored in raycasthit.

avatar image meat5000 ♦ · Aug 09, 2013 at 03:31 PM 0
Share

If the ray comes from the screen then RaycastHit.distance is equal to what you want, no? For some reason I was assu$$anonymous$$g the ray came from the player, not the camera.

 if(Physics.Raycast(look, out hit, 5.0F))
 {
      Debug.Log(hit.distance);
 }

Just noticed, the 5.0F is a ray distance. It means the ray is only cast 5 units and so might not hit anything. Leave it out for an infinite ray and see if that makes a difference.

Added to answer.

avatar image YoungDeveloper · Aug 10, 2013 at 08:02 PM 0
Share

Hey, ha, i already posted that long time ago but thanks.

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.

Follow this Question

Answers Answers and Comments

15 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

Related Questions

Keep camera at certain distance from character when rotating 3 Answers

find a world-space coordinate from ScreenPointToRay(Input.mousePosition) raycast 2 Answers

Set Distance to object 1 Answer

Vector3(x,y,variable)? 0 Answers

Need to flatten a 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