Raycast not going desired direction.

I have a top down view. I’m trying to cast a ray one unit away from the player in the direction they are facing. But instead of the ray going forward it is going to a seemingly random point in toward the middle of the screen. And said point moves on direction keydowns some times.

function Update (){
    var direction = transform.TransformDirection(Vector3.forward);
    var hit : RaycastHit;

	if (Physics.Raycast (this.transform.position, direction, hit, 1)) //cast ray
	{
			print("HIT");
	}
    Debug.DrawLine (this.transform.position, direction, Color.white);

}

And ahead of time, Thanks for any help. :slight_smile:

I’m pretty sure the ray is going in the right direction. You’re not using the corresponding debug function though. DrawLine takes (pointA, pointB) so it must have been pointing near (0,0,0) every times. Use DrawRay instead.

Now, just some remarks :

  • transform.TransformDirection(Vector3.forward) is already stored as transform.forward.
  • you don’t need “this.” to access transform.XXX. It doesn’t hurt I give you that, but the less code there is, the happiest the programmer is !