- Home /
Why is the ray cast pointing towards the origin of the scene?
I want the raycast to be pointing forward along the x axis of a cube in my scene. Instead the raycast starts at the origin of the cube and then points towards the origin of the scene (0,0,0). The length of the raycast is as long as it needs to be in order to reach the origin, despite me giving it a specific length.
void Update () {
RaycastHit hit;
Vector3 fwd = transform.TransformDirection(Vector3.forward);
Debug.DrawLine (transform.position, transform.forward, Color.red);
if (Physics.Raycast (transform.position, fwd, out hit, 10)) {
print ("There is something in front of the object!");
}
}
Answer by Bunny83 · Oct 11, 2016 at 12:25 AM
Your problem is that you use Debug.DrawLine
which expects two worldspace points. However you pass one worldspace point and a direction vector. Direction vectors are normalized (have a length of 1f). So since you treat the direction vector as position it's always around the origin (in the range +-1 on each axis).
Either use Debug.DrawRay like this:
Debug.DrawRay(transform.position, transform.forward*10f, Color.red);
where "10" is the length of your ray, or use DrawLine like this:
Debug.DrawLine (transform.position, transform.position + transform.forward*10f, Color.red);
which does the same thing. Your actual raycast was correct but your visualization wasn't.
I feel kind of silly knowing that the problem had to do with the debug statement, but this fixed the problem. thanks for the help.
Answer by tanoshimi · Oct 10, 2016 at 09:25 PM
"forward along the x axis"? Do you mean pointing right? Forward is the z axis. Anyway, try this
if (Physics.Raycast (transform.position, transform.forward, out hit, 10)) {
Answer by pcdrive · Oct 10, 2016 at 09:40 PM
Hi dude!
I think i know what's wrong, but ima beginner too =P. I dont really understand what you wanted to do. If you wanted to check raycast in the x axis from the object or front of the object. but its almsot the same. just use transfrom.direction for the forward or trunk it more to get its x value to check the x axis =).
Sorry for poor english... not my native.
why you want to transform the direction? i bet the problem is there. I use raycast in collision avoidance ai system with the same generation. you should jsut simply use the ray construktor as Ray ray = new Ray (gameObject.transform.position, gameObject.transform.forward); and it will make you a ray that will be started at the center of your gameObject and points towards the object forward vector. After that you can check wha it hits easily.
I hope i could help.
Good luck
Answer by jrocamora · Oct 11, 2016 at 12:18 AM
transform.TransformDirection doesn't return the transform's world coordinates, just a Vector3 that equates to what direction you want in relation to the object, at least that's what I think because I've had a similar problem to yours before.
Try changing your Vector3 fwd to this:
Vector3 fwd = transform.position + transform.TransformDirection(Vector3.forward);
While it is correct that a direction is just a direction vector which has to put in relation to some point in space, your answer is misleading. He doesn't use his "fwd" variable for the "drawing" only in his actual Raycast line. Physics.Raycast needs a direction and not a position as second parameter.
Also
transform.TransformDirection(Vector3.forward)
is exactly the same as
transform.forward
I changed "fwd" to transform.forward, and it works fine now. Thank you.
Your answer
Follow this Question
Related Questions
Transform Object over Players head before destroying 0 Answers
How to have an object some distance above another object Locally? 1 Answer
c# How do I set up continuous raycast collision detection? 1 Answer
Raycast Mouse Click On Specific Objects Only 0 Answers
Creating 2d image map of objects based off 3d space colliders Unity C# 0 Answers