Quick Question about Vector3 and transform.TransformDirection(Vector3)

The transform.TransformDirection(Vector3) function takes local VectorA. Changes its direction and position based on the rotation and world position of the transform.

If I wanted to replicate this function and change the direction and position of local VectorA. Instead, based around the direction of VectorB. And the position of VectorC. What would be the most optimal way to do this?

Transform.TransformDirection() only rotates the vector, it ignores translation. The method simply converts the transform’s world-space quaternion to a 3x3 matrix and then multiplies the given vector to that matrix. The algorithm can be demonstrated thus…

(Note: While this is functionally identical to Transform.TransformDirection(), it is almost certainly less optimized.)

	Vector3 TransformDirection(Transform transform, Vector3 v) 
	{
	   Matrix4x4 m = Matrix4x4.TRS(Vector3.zero, transform.rotation, Vector3.one);
	   return m.MultiplyPoint3x4(v);
	}

I’m not sure what you mean by “based around the direction of VectorB.” You need more than just VectorB to define the rotation. At the very least, you need a rotation angle around VectorB, from which we can infer the other two axes of the coordinate system.

You always need three vectors to define orientation in 3D space: the x-axis, y-axis, and z-axis vectors. It’s common to write functions that take in only two of those axis vectors and computes the third axis vector automatically. For example, you’ll notice that the Quaternion.LookRotation() method only takes a forward vector (z-axis) and an up vector (y-axis) as input, it calculates the right axis (x-axis) vector from the cross product of the forward and up vectors and then builds an orthogonal basis for the rotation.

You can use Quaternion.LookRotation() to write a function that transforms a point into a coordinate system defined by a forward vector (z-axis), an up vector (y-axis), and a position vector. Maybe this is closer to what you had in mind.

	// transform vector v into the coordinate system defined by forward, up, and position.
	Vector3 TransformToForwardUpPosition(Vector3 forward, Vector3 up, Vector3 position, Vector3 v)
	{
		Quaternion rotation = Quaternion.LookRotation(forward, up);
	   	Matrix4x4 m = Matrix4x4.TRS(position, rotation, Vector3.one);
	   	return m.MultiplyPoint3x4(v);
	}

As Brian said, your question is a bit odd. If you jsut want to copy the rotation of one transform and the position of another, this will work:

Transform tA
Transform tB
Transform tC

tA.forward = tB.forward;
tA.position = tC.position;

Thanks for the thorough reply Brian. Sorry though, I was very tired when I wrote that question.

I know TransformDirection(Vector3) doesn’t change the Vector’s position based on the position of the transform. That was dumb of me. It is only affected by the rotation of the transform.

My question was. If I have Vector3A, and want to transform its direction based on the X and Y angles from Vector3.zero to Vector3B, with a Z rotation (Roll) of 0, what would be the most optimal way.

I could solve this using 2root(), atan2(), sin(), and cos() functions. But that seems far from optimal in Unity.

Is the function you laid out here what I am looking for? Seems like it might be, but my knowledge of Quaternions and Matricies is far from what it should be.

Then Quaternion.LookRotation() function is exactly what you want. It assumes that the rotation will include no roll. You give it a look direction (forwad vector) and an up vector. The resulting rotation will be around the “up” axis and around an axis parallel to the plane that the up vector is normal to. It is analogous to a gun turret, or an altitude-azimuth (alt-az) mount.