- Home /
What is wrong with my raycast
Hi
I'm trying to cast a ray from the camera straight down. How would I do this?
My code
Ray rayDown = new Ray(transform.position, Vector3.down );
RaycastHit hitDown;
if(Physics.Raycast(rayDown, out hitDown, Mathf.Infinity))
Debug.DrawRay(rayDown.origin, hitDown.point, Color.red);
The ray starts off going from the camera straight down but if the camera moves the ray will angle its self and will no longer be pointing down.
Thanks
Raycasts are always in world co-ordinates. Do you want it to go straight forward along your line of sight?
Nope from the cameras position, to the ground. If i use Vector3.down it doesn't go straight down.
The guy from the first comment is right, It's aways world coordinates.
When I move the camera, the hit point of the ray moves even further if that makes sense. To the point where it will go from horizontal to vertical.
Answer by whydoidoit · Oct 06, 2012 at 04:58 PM
Your problem is not the ray cast its the Debug.DrawRay
The way you are calling it - the second parameter should be the (hit.point - rayDown.origin) as it is a direction not a point.
Sorry, that was slightly confusing. What do I need to make the second parameter ?
if(Physics.Raycast(rayDown, out hitDown, $$anonymous$$athf.Infinity))
Debug.DrawRay(rayDown.origin, hitDown.point - rayDown.origin, Color.red);
Oh Doh! I thought they were dashes not $$anonymous$$us signs. Ha, Thanks worked great.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Mouseclick GameObject through the view of a RenderTexture 0 Answers
Camera variable problems 0 Answers
Distribute terrain in zones 3 Answers
Good Game by One Person, Is it Possible? 2 Answers