- Home /
Raycast transform.forward always going to global (0,0,0)
hey
I am trying to figure out why my ray cast transform.forward is always going to global transform vector 0,0,0
well thats what the debug.drawline seems to be representing.
sorry if blue line hard to see
heres my code:
Ray ray = new Ray (transform.position, transform.forward);
RaycastHit hit;
float distanceToPlayer;
if (Physics.Raycast (ray.origin, ray.direction, out hit))
{
Debug.DrawLine (ray.origin, transform.forward, Color.blue);
transform.LookAt (player.position + transform.up);
//Debug.DrawLine (ray.origin, player.position, Color.green);
Debug.DrawLine (ray.origin, player.position, Color.white);
distanceToPlayer = hit.distance;
Debug.Log (distanceToPlayer);
}
The rest of it works well... and it seems to be the way everyone else is doing it, so can someone please explain?
Thanks :-)
Answer by zach-r-d · Jun 22, 2015 at 01:48 AM
Sure, the problem is that the code is using Debug.DrawLine instead of Debug.DrawRay to draw a ray.
Debug.DrawLine takes two points in world space. So while the start of the line is correctly at the ray origin, the end of the line is at a point somewhere 1 unit away from (0,0,0) because directions are represented as normalized vectors. Debug.DrawRay will instead use the direction as a direction instead of a point, automatically calculating the end point of the line in world space based on the starting position and the direction.
Also, a quick aside, you can pass the Ray directly to Raycast:
if (Physics.Raycast(ray, out hit))