Rotate Vector to face direction

Hi quick question:

I have a vector which will be applied by a force.

I want to deflect that force to face a different direction.

I have a normalized vector which faces another direction and I want to rotate my original force to face the same direction as the normalized vector, maintaining its original scale obviously.

Ive come up with a solution but am still interested in how to do this for other problems.

Thanks in advance.

the new vector would be the normalized vector * old vector magnitude.

newVector = normalizedDirection*oldVector.magnitude;

If you want to control the speed of rotation, you can use this code (I use this for turning an agent with a rigidbody):

// Turn
Vector3 desiredLookPos = seekPos;
desiredLookPos.y = transform.position.y;
Vector3 desiredLookDir = desiredLookPos - transform.position;
desiredLookDir.Normalize();
float rotationDeltaInRadians = m_rotationSpeed * Time.deltaTime;
Vector3 newForwardDirection = Vector3.RotateTowards(
transform.forward,
desiredLookDir,
rotationDeltaInRadians,
0.0f);
transform.forward = newForwardDirection;

And you should add a public variable called m_rotationSpeed, to this component. This value will control how fast your agent rotates.

If you want to control the angular acceleration, I can show you how to do that as well.

Thanks guys, that’s what I wanted Ivkoni, sometimes my lack of a maths background lets me down, so simple!

@alex, is that faster than just using Quaternion.RotateTowards()?

This is a SLERP but with a fixed (max) speed. Normally I would be weary of SLERP but I recently switched to it since it is easier to work with and I didn’t see a performance difference. All the vector/angle work versus a single SLERP might be comparable. It would be interesting to see a comparison with one of your big pathing tests! Game?

I didn’t know you could set “transform.forward”, pretty cool.