diff --git a/Assets/Scripts/Player/UnitMovementHandler.cs b/Assets/Scripts/Player/UnitMovementHandler.cs index b41c05b..1a1454c 100644 --- a/Assets/Scripts/Player/UnitMovementHandler.cs +++ b/Assets/Scripts/Player/UnitMovementHandler.cs @@ -69,6 +69,8 @@ namespace Reset.Units{ [ShowInInspector, ReadOnly] private Quaternion outputRotation; [ShowInInspector, ReadOnly] private Quaternion specifiedRotation; [ShowInInspector, ReadOnly] private float outputRotationSpeed; + + private float directionChangeDot; private CharacterController controller; private PlayerControls controls; @@ -149,16 +151,36 @@ namespace Reset.Units{ Vector3 targetNoY = new Vector3(targetDirection.x, 0f, targetDirection.z); Vector3 currentNoY = new Vector3(outputMoveDirection.x, 0f, outputMoveDirection.z); + // Also need to find the dot value of the current input versus the current move direction + Vector3 slerpedValue; + Vector3 lerpedValue; + + float switchedDirection = Vector3.Dot(inputMovement, outputMoveDirection); + float switchedDirectionRemapped = Mathf.Lerp(0, 1, switchedDirection); + + directionChangeDot = Mathf.Lerp(switchedDirection, switchedDirectionRemapped, 5f * Time.deltaTime) ; // turn that .5f into a variable + // Smooth movement. Use deaccel smoothing if the input magnitude is lower, and accel smoothing if it's higher - // Also checks when grounded to only use Slerp on the ground + // Also checks when grounded to only use Slerp on the ground + if (targetNoY.magnitude > currentNoY.magnitude) { - currentNoY = controller.isGrounded ? - Vector3.Slerp(currentNoY, targetNoY,data.accelerationSmoothing * Time.deltaTime) : - Vector3.Lerp(currentNoY, targetNoY,data.accelerationSmoothing * Time.deltaTime); + if (controller.isGrounded){ + slerpedValue = Vector3.Slerp(currentNoY, targetNoY, data.accelerationSmoothing * Time.deltaTime); + lerpedValue = Vector3.Lerp(currentNoY, targetNoY, data.accelerationSmoothing * Time.deltaTime); + + currentNoY = Vector3.Lerp(lerpedValue, slerpedValue, directionChangeDot); + } else { + currentNoY = Vector3.Lerp(currentNoY, targetNoY, data.accelerationSmoothing * Time.deltaTime); + } } else { - currentNoY = controller.isGrounded ? - Vector3.Slerp(currentNoY, targetNoY, data.deaccelerationSmoothing * Time.deltaTime) : - Vector3.Lerp(currentNoY, targetNoY, data.deaccelerationSmoothing * data.airDirectionDecay * Time.deltaTime); + if (controller.isGrounded){ + slerpedValue = Vector3.Slerp(currentNoY, targetNoY, data.deaccelerationSmoothing * Time.deltaTime); + lerpedValue = Vector3.Lerp(currentNoY, targetNoY, data.deaccelerationSmoothing * Time.deltaTime); + + currentNoY = Vector3.Lerp(lerpedValue, slerpedValue, directionChangeDot); + } else { + currentNoY = Vector3.Lerp(currentNoY, targetNoY, data.deaccelerationSmoothing * data.airDirectionDecay * Time.deltaTime); + } } // Commit move direction