- Home /
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)
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.
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.
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.
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"){
....
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.
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.
Hey, ha, i already posted that long time ago but thanks.
Your answer
Follow this Question
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