- Home /
Moving A Ray Relative To Parent, Not World
How can I make it so the ray starts a certain distance away from the front of an object. So when the object rotates, the ray start point stays in the same relative position to the object.
var ray = new Ray(transform.localPosition + Vector3.forward * 10, transform.TransformDirection(Vector3.forward));
That was my attempt. But it only moves it based on a global axis.
Thanks for you time, Magnus :)
Answer by robertbu · Nov 26, 2013 at 10:13 PM
You can use the transform.forward.
var ray = new Ray(transform.position + transform.forward * distanceAway, transform.forward);
Note I don't understand your use of 'localPosition' in the above code.
Woops, yeah. That was originally just 'position'. I left it in there by mistake after playing around :P. Ill try your suggestion out, thanks
yeha 'transform.foward' is what I was after,not 'Vector3.forward' :P
Thanks man :)
Also if I wanted to change the direction/rotation of the ray relative to its parent, what could I do?
Quaternion.AngleAxis(20, transform.up) * transform.forward
dont worry, found it :P
You can change it to anything you want. It is the second parameter in the new Ray() call, plus you can assign to Ray.direction. But 'relative to its parent' is not enough information for me to give you a line of code for what you are trying to do. If you spell out exactly what you want, I'll take a stab and telling you how you can achieve it.