- Home /
heeelp!, i need to cast a ray from transform local position to "down" in local position
Well, im struggling with this for days now, kinda depressing.
If i cast this way:
ray = new Ray(this.transform.localPosition, this.transform.position.down); // arma el rayo
Debug.DrawRay (this.transform.localPosition, this.transform.position.down * 20, Color.green);
I get a ray coming down when my vehicle is in a normal position, but when i flip it over a slope i cant get the ray to cast down the vehicle, i just get a ray "down" i world coords.
I tried with this and it works in 70% of slopes i try, but the ray is going diagonal and get strange behaviours when i have the vehicle flipped upside down, i seriously dont get this:
ray = new Ray(this.transform.localPosition,worldToLocalMatrix.MultiplyVector(this.transform.position.down));
Any help ASAP is really appreciated.
Answer by robertbu · Aug 20, 2014 at 06:13 AM
You want 'transform.down' to get a ray going in the direction that the bottom side of your vehicle is facing. And I'll bet you want transform.position and not transform.localPosition;
ray = new Ray(transform.position, transform.down);
Debug.DrawRay(transform.position, transform.down);
Note that 'this'transform.position.down' is the same as Vector3.down, which is the world down.
Well, transform.down is not working for me. But, the first example in the docs actually does what i need. Thanks robertbu.
http://docs.unity3d.com/ScriptReference/Physics.Raycast.html
Note if the sample in the docs work for you, then the side you have 'down' is the forward side. You can change the code above to use transform.forward, ins$$anonymous$$d of transform.down. That is:
transform.TransformDirection(Vector3.forward)
...is the same as:
transform.forward
Your answer
Follow this Question
Related Questions
World coordinates from NDC - Raycast 0 Answers
2D Platform Player moving instantly from upper platform to lower, only when moving left 0 Answers
Drifting Raycast in Camera? 2 Answers
What does Physics2D.Raycast return? 0 Answers
Health System Raycast Bullets 2 Answers