Using Quaternion orientation with a direction vector
I have a position in space and I want to shoot rays around that position (we'll say in a box like manner for this example) so say I have a default cube and I want to shoot a ray from each corner on the forward face in the forward direction of the cube, now lets say the cubes rotation is (0, 45, 45). How would I shoot rays from each corner of that cube in the forward direction?
I can't quite figure out how to use the orientation to get the 4 points around the center position. I know I can get the center of the forward face by using transform.forward * (transform.localScale.x / 2). After that the math needs the orientation information to get the proper coordinates. I don't need anyone to code it for me. I just need to understand how the math works for using the orientation.
Thanks in advance for the help!
Answer by Bunny83 · Sep 13, 2016 at 09:29 PM
You basically have two options:
Use a combination of transform.forward / right / up to get your desired starting points
Or what's way easier, use local coordinates and transform the final position into worldspace using
transform.TransformPoint
So the 4 corners on the front face of a default cube are:
new Vector3( 0.5f, 0.5f,0.5f); // right top
new Vector3(-0.5f, 0.5f,0.5f); // left top
new Vector3(-0.5f,-0.5f,0.5f); // left bottom
new Vector3( 0.5f,-0.5f,0.5f); // right bottom
So just run them through transform.TransformPoint()
and you get the appropriate worldspace position. TransformPoint does apply the rotation, scale and position of the transform.
edit
In case you don't have a transform that faces your desired direction but only a Quaternion you have to do the the 3 things manually. So first multiply / scale those 4 local points with your desired scale. You can use Vector3.Scale for that. Once the local vectors have to proper length you would rotate them using the quaternion. Finally add the result to a worldspace center position.
var worldSpacePoint = worldSpaceCenter + quat * Vector3.Scale(yourLocalPoint, yourScaleVector);
Also thanks for the equation in the edit, it's exactly what I needed!
Your answer
Follow this Question
Related Questions
Determining if my target is standing upright 4 Answers
How can I find direction with known 2 projection angles on xz and yz planes? 2 Answers
The direction my camera is rotating should indicate the direction my object facing 0 Answers
Unity 3D C# - Rotation To Slope Of Terrain 0 Answers
Rotating an Object (To Face Another Object) Only on X and Y Axis 3 Answers