What is the best way to ease a rotation? (js)

I have a gameObject, Player Controller. This gameObject handles all of the movement, which basically moves you in the direction of the input relative to the screen. Inside the Player Controller I have the gameObject playerAvatar as playerConroller’s child which has the model with all the animations and whatnot. (playerController —> playerAvatar)

In combat you have option to target an enemy, in which case the playerAvatar faces the target. I used this code in the update function to accomplish this:

targetGetRot = Quaternion.LookRotation(battleTarget.transform.position - transform.position);

targetFacingRot = targetGetRot.eulerAngles.y;

Also in the update I have a boolean variable enemyTargeted that looks like this:

if(enemyTargeted && !rotationRestricted){

playerAvatar.transform.eularAngles.y = targetFacingRot;

}

It all functions perfectly up to this point. I can turn the enemyTargeted boolean on an off to have the character face the enemy, and set the playerAvatar y value to 0 to have him facing the direction he’s moving when he’s not targeted.

The issue I am having is that when the character attacks or casts a spell, the targeting rotation gets turned off and he keeps his facing direction with the rotationResticted variable (boolean). However when I want to put the targeting back on I can’t find an effective way to tween the rotation back to targeted. Since the enemy is moving, even when I try to use iTween there is a hiccup from where the rotation was when the tween started to the targetFacingRot when its finished. I tried to use a Lerp in the update function but the results made the playerAvatar spin in all kinds of different directions before it landed on the correct rotation.

Does anyone know an effective way to do this without making the transition look like a slideshow? Any help or nudges in the right direction would be greatly appreciated.

Do the ‘targetGetRot’ this way.

 var v = battleTarget.transform.position - transform.position;
 v.y = 0.0;
 targetGetRot = Quaternion.LookRotation(v);

Then you are going to do the rotation one of the following two ways:

if (enemyTargeted && !rotationRestricted) {
     playerAvatar.transform.rotation = Quaternion.Slerp(playerAvatar.transform.rotation, targetFacingRot, speed * Time.deltaTime);
 
 }

…or:

if (enemyTargeted && !rotationRestricted) {
     playerAvatar.transform.rotation = Quaternion.RotateTowards(playerAvatar.transform.rotation, targetFacingRot, speed * Time.deltaTime);
 }

‘speed’ is a variable you declare and assign. In the first form, typical values are 3 - 10 and produce an eased rotation. In the second form, ‘speed’ will be degrees per second and therefore be a constant speed rotation.