# Rotate but keep direction

I’m having trouble with rotating. The player movesw from one platform to the next and faces the same direction. I thought I had it working fine but it seems platforms with certain rotations do not work.

Moving from A → B causes him to face back towards A, however, if B = (0,345,0) it works as expected.
Moving from A → C works
Moving from C → D causes him to also face back towards C

Basically, I want him to move to the platform infront and take on it’s direction but still face in the correct direction.

I’ve tried several things but all don’t work.

``````look = Quaternion.LookRotation(transform.forward);
targetRotation = hit.transform.rotation * look;
``````

Okay, I think I’ve solved it with the below. If the destination’s opposite angle is a shorter turn than going to the actual destination, then rotating towards that seems to keep him facing in the correct direction.

``````// Figure out which was to turn whilst still maintaining his direction.
// EG. turning from 0->345 is correct, turning from 0->165 (345's opposite) is closer
// but doesn't keep his original direction.
targetRotation = hit.transform.rotation;
float currentAngle = transform.rotation.eulerAngles.y;
float destAngle = hit.transform.rotation.eulerAngles.y;

// If the current and destination is rotated by 90 degrees then ignore it and don't rotate.
// Round the angle since he might be 0.0001 off which is enough to break the MOD.
float destFloor = Mathf.Floor(destAngle % 90);
float currentFloor = Mathf.Floor(currentAngle % 90);
if (currentFloor == 0f && destFloor == 0f)
{
targetRotation = transform.rotation;
return;
}

// Get the 180 angle of the destination.
float one80;
if (destAngle - 180f < 0)
{
one80 = destAngle + 180f;
}
else
{
one80 = destAngle - 180f;
}

// Calculate the angle between A to B and A to B's 180 and take the shortest.
float angle2Dest = Mathf.Abs(Mathf.DeltaAngle(currentAngle, destAngle));
float angle2180 = Mathf.Abs(Mathf.DeltaAngle(currentAngle, one80));
if (angle2Dest < angle2180)
{
targetRotation = targetRotation;
}
else
{
targetRotation = Quaternion.Euler(new Vector3(targetRotation.eulerAngles.x, one80, targetRotation.eulerAngles.z));
}
``````