Ray.direction gives an x,y and z value between 1 and -1. is there a way to get euler angle that the ray has been rotated?
1 Answer
1Ray.direction is a Vector3. The values are between 1 and -1 because it has been normalized (i.e. the length of the vector will be 1.0). Angles are more complicated in a 3D environment, so “get euler angle” is not clear. You need to define your frame of reference. You could use Quaternion.FromToRotation() to create a rotation and then read the euler angles from the rotation. You need to select the ‘from’ to use in the calculation. The following code uses Vector3.up.
Quaternion q = Quaternion.FromToRotation(Vector3,up Ray.direction,);
Vector3 v3Euler = q.eulerAngles;
There are multiple eulerAngles for any physical rotation, so the euler angles you get back may not be the ones you expect.
hey robertbu, thanks for the answer. i understand now that my question was a bit silly now. what i am trying to do is get an empty object to be placed at the ray.origin with the z axis facing the same as the world coord. which i can do, i then want the X axis to point in the direction of the ray, so that i can get the z axis euler angle. also, both the start point of the ray and the finish point of the ray will be on the same plane. sorry if that doesnt make sense heh.
– Proportion1I cannot fully visualize what you want. In particular I cannot see how z can face world z and the x also point along the direction But here is a suggestion. Use two empty game objects. The parent object will have positive 'Z' point along direction. The child object will have a local rotation with respect to the parent that gives you your 'X' facing in direction. Doing it this way, you can do this to the parent: transform.Lookat(transform.position + ray.direction);
– robertbuthats the one. thanks heaps robert
– Proportion1