private Vector3 ApplyInputVelocityChange(Vector3 velocity)
{
if (!canControl)
inputMoveDirection = Vector3.zero;
// Find desired velocity
Vector3 desiredVelocity;
if (grounded && TooSteep()) {
// The direction we're sliding in
desiredVelocity = new Vector3(groundNormal.x, 0, groundNormal.z).normalized;
// Find the input movement direction projected onto the sliding direction
Vector3 projectedMoveDir = Vector3.Project(inputMoveDirection, desiredVelocity);
// Add the sliding direction, the spped control, and the sideways control vectors
desiredVelocity = desiredVelocity + projectedMoveDir * sliding.speedControl + (inputMoveDirection - projectedMoveDir) * sliding.sidewaysControl;
// Multiply with the sliding speed
desiredVelocity *= sliding.slidingSpeed;
}
else
desiredVelocity = GetDesiredHorizontalVelocity();
if (movingPlatform.enabled && movingPlatform.movementTransfer == MovementTransferOnJump.PermaTransfer) {
desiredVelocity += movement.frameVelocity;
desiredVelocity.y = 0;
}
if (grounded)
desiredVelocity = AdjustGroundVelocityToNormal(desiredVelocity, groundNormal);
else
velocity.y = 0;
// Enforce max velocity change
float maxVelocityChange = GetMaxAcceleration(grounded) * Time.deltaTime;
Vector3 velocityChangeVector = (desiredVelocity - velocity);
if (velocityChangeVector.sqrMagnitude > maxVelocityChange * maxVelocityChange) {
velocityChangeVector = velocityChangeVector.normalized * maxVelocityChange;
}
// If we're in the air and don't have control, don't apply any velocity change at all.
// If we're on the ground and don't have control we do apply it - it will correspond to friction.
if (grounded || canControl)
velocity += velocityChangeVector;
if (grounded) {
// When going uphill, the CharacterController will automatically move up by the needed amount.
// Not moving it upwards manually prevent risk of lifting off from the ground.
// When going downhill, DO move down manually, as gravity is not enough on steep hills.
velocity.y = Mathf.Min(velocity.y, 0);
}
return velocity;
}