private void RotateWithinThreshold() { // Angle between character forword vector and the target. float hAngle; // Angle between custom selected transform and the target. You can // select custom transtorm in the inspector. float hAngleCustom; // How much the target is beyond the specified threshold (in // degrees). float hAngleThr; // Rotation that will be applied to the transform. When rotation is // finished, should always be 0. float newRotation = 0; hAngle = AngleAroundAxis( MyTransform.forward, targetTransform.position - MyTransform.position, Vector3.up); if (overrideRoot) { hAngleCustom = AngleAroundAxis( overrideRoot.forward, targetTransform.position - overrideRoot.position, Vector3.up); } else { hAngleCustom = AngleAroundAxis( MyTransform.forward, targetTransform.position - MyTransform.position, Vector3.up); } // On click, omit all the smoothing code and rotate transform // immediately. if (Input.GetKey(KeyCode.Mouse0) && clickInstantRot) { MyTransform.Rotate(0, hAngleCustom, 0); return; } // If target is within specified threshold, return 0 which means, // that there's no reason to rotate. hAngleThr = Mathf.Max( 0, Mathf.Abs(hAngle) - thresholdAngle); // If target is beyond specified threshold, add a sign to know // which direction to rotate. hAngleThr *= Mathf.Sign(hAngle); // Rotation to be applied. newRotation = Mathf.SmoothDampAngle( newRotation, hAngleThr, ref velocity, minTimeToReach, maxRotSpeed); // Apply rotation to the transform. MyTransform.Rotate(0, newRotation, 0); }