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!

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);