Multiplying Quaternions To Reset a rotation;

I am creating the enemy AI for my game. I am changing the position of the spine, chest and upper chest to make sure the AI points the gun at the player. (I’ve added restrictions which work to make spine go back to default position if the player is behind the AI but that’s using vectors which I understand) The problem I am facing is when trying to use Quaternions to change the player rotation.

(function used to rotate bones)

private void AimAtTarget(Transform bone, Vector3 targetPosition, float weight)
        Vector3 aimDirection = gunEnd.forward;
        Vector3 targetDirection = targetPosition - gunEnd.position;
        Quaternion aimTowards = Quaternion.FromToRotation(aimDirection, targetDirection);
        Quaternion blendedRotation = Quaternion.Slerp(Quaternion.identity, aimTowards, weight);
        bone.rotation = blendedRotation * bone.rotation;

I want to blend each bone between default position and facing the player position using the Slerp (as currently the spine is the first to rotate and its getting fully rotated towards the player so the other 2 don’t get rotated, hopefully that makes sense). Currently it faces the player when weight > 0 but when weight is 0 (should be at 0 rotation as it’ll then be multiplying the bone rotation by quaternion.identity) it instead stops rotation and just stays in the same angle (doesn’t follow player but isn’t facing start position/ no rotation)

Can anyone help spot the problem, I think its the multiply quaternion.identity with the bone roation as I’ve debug.log(blendedRotation) and when weight = 0 it comes out as 0, 0, 0, 1 (whihc is quaternion.identity)


If you want a weight of 0 to reset the character to its start position, why not set rotation to quaternion.identity (or whatever it’s starting rotation was) directly in that scenario.

When lerping toward some value like you are, it is expected behavior for a weight of 0 to “pause” it. Try thinking of the value you passed to the lerping function not as a weight, but more like a speed - a value of 1 makes it instantly face the player, and a value of 0 is not moving -, it has no explicit concept of a starting position, just the current position and the target position.

Notice that while you do use the * symbol for quaternions like quaternion.identity, I wouldn’t compare it to multiplying by 0, but rather adding 0 - it does nothing -, this is why your character stops when weight is zero.

Also note that when combining quaternions, a * b is not the same as b * a.

I’m not entirely sure I understood your problem, so please correct me if I misunderstood, but I hope this helps :slight_smile: