Rotate Local Movement Toward Target Point

I have an object with a character controller component that moves to a target position (represented as the red gizmo sphere). This object has a movement vector given in local space using animation curves to apply root motion during an animation, such as a dodge. I have purposefully made the object’s position lag behind the target position to an extra extent to debug this. My question is about rotation. I have a movement vector, represented in local space, but because we already have a target position, we want to move the object toward that target position, using the movement vector. If I didn’t have a target position, I would simply just do the following:

movement = transform.rotation * movement;

This would convert the movement to world space and allow me to use it in the CharacterController.Move() method. However, because I have a target position, I need to change the rotation to ignore the object’s current rotation (it can change during the movement), and rotate the movement vector to reach the target position.

The solution was to take account for the direction of movement. This is because the movement vector includes, forward, vertical, and horizontal movement, not just forward. So the solution was as follows:

// Calculate rotation to look at the current network position
Quaternion relativeRotation = Quaternion.identity;
if ((networkTransform.currentPosition - networkTransform.transform.position).normalized != Vector3.zero) { relativeRotation = Quaternion.LookRotation((networkTransform.currentPosition - networkTransform.transform.position).normalized, networkTransform.transform.up); }
  
// Calculate rotation to look in the direction of our movement
Quaternion movementRotation = Quaternion.identity;
if (movement.normalized != Vector3.zero) { movementRotation = Quaternion.LookRotation(movement.normalized, networkTransform.transform.up); }

// Apply the direction of movement to the direction to move towards the current network position
Quaternion finalRotation = relativeRotation * movementRotation;

// Invert movement along the local x axis, idk why I need to do this
movement.x *= -1;
// Apply rotation to movement vector
movement = finalRotation * movement;