- Home /
How to make sure that the raycast points in the right direction?
So this the script I currently am using but for some reason the raycast always points at other direction
var Hit : RaycastHit;
Debug.DrawLine( transform.localPosition, transform.forward * 100 , Color.blue);
if( Physics.Raycast( transform.localPosition, transform.forward * 100, Hit))
{
Debug.Log(Hit.collider.name);
}
This is the image. As you can see, the raycast is not pointing along the local z-axis as it was meant to.
][1]
Answer by robertbu · Mar 11, 2014 at 11:29 PM
The major problem here is that you are using Debug.DrawLine() which takes two positions, but you are passing it a position and direction. Change to DrawRay():
Debug.DrawRay( transform.position, transform.forward * 100 , Color.blue);
The second problem is that Rays are cast in world space, but you are using 'localPosition' for both the Raycast() and the drawing. Change to using 'transform.position' in both places.
Your answer
Follow this Question
Related Questions
How to Spherecast/Raycast diagonally? 1 Answer
Raycast not detecting distance of my GameObject 1 Answer
Clicking on an Object to Make it the Variable Target 1 Answer
Raycast Pause Between Hits? 0 Answers
Go Idle after Animation 0 Answers