- Home /
How to point a Ray in the right direction?
I'm having issues with my raycasts not returning the expected result. I've added a Debug.DrawLine, and sure enough, my line isn't going forward, but is always pointing down/right, as you can see in the screen shot. (Screenshot: The capsule is the player, facing towards the green box, but the red-line, the ray, is going down right.)
I've tried manually entering the vector3 numbers, changing them dramatically, but the line always points the same direction.
Debug.DrawLine(transform.position, Vector3.forward, Color.red, 5);
What am I doing wrong? Thanks!!

Answer by Wolfram · Feb 03, 2013 at 09:26 PM
DrawLine expects two points, not a point and a direction.
Ah, I see. I suppose then I should ask, how can I visualize the ray for my character? I'm doing this:
if (Physics.Raycast (transform.position, raydirection, hit, 1)) {
raydirection is Vector3.forward. However, it seems off -- it's catching things that aren't in front of the character and often not catching things that are. So I was trying to get a visual as to what the raycast is actually shooting towards.
Umh, I suggest reading up on basic vector arithmetics then... ;-)
DrawLine(transform.position,transform.position+length*raydirection);
However, Vector3.forward is always the Z world axis, so once you turn your object it will be wrong. You probably want to use transform.forward ins$$anonymous$$d, which is the local Z of that transform.
Your answer
Follow this Question
Related Questions
Logic Required 1 Answer
Follow mouse until mouseclick 0 Answers
Attaching 2 objects using joints and raycasting 0 Answers