rotation issue - rotate parent to align child with line from parent towards direction

Say I have parent and a child transform. The child has an arbitrary local position that isn’t the local origin.

Now, say I’m given some directional vector and I draw a line from the parent, towards that direction, in world space.

How do I rotate the parent so that the child ends up aligned with that line? (Between the parent and some imaginary point at parent.pos + direction * infinity)

It has to be a rotation. I can’t just relocate the child unfortunately. I’m blanking.

If it helps, the actual use case is to have a character aim in a direction. I don’t create models myself and the upper arm bones local forward is rarely suitable for LookAt. My usual go-to is to edit the bones but I’d like to find an alternative as I find myself doing that quite a lot.

My idea is that my theoretical “forward” direction could be the forearm.position - upperarm.position instead, and somehow rotate that.

This should do it:

Vector3 currentDir = child.position - parent.position;
Vector3 targetWorldDir = <whatever>;

parent.rotation *= Quaternion.FromToRotation(currentDir, targetWorldDir);
1 Like

Thank you, that did it!