[Tooltip("Each frame while jumping, gravity will be multiplied by this amount in an attempt to 'cancel it' (= jump higher)")] public float gravityDivider = . 6f ;
[Tooltip("Adjust the friction of the slope")] public float slideFriction = 0.3f ;
private float currentSlope ;
//Raises the multiplier to how much gravity will affect vertical movement when in mid-air
//This is 0f at the beginning of a jump and will raise to maximum 1f
if ( ! characterController . isGrounded )
//Calculate the final verticalMovement
if ( ! characterController . isGrounded )
{
//The character is either jumping or in freefall, so gravity will add up
gravityContributionMultiplier = Mathf . Clamp01 ( gravityContributionMultiplier ) ;
verticalMovement + = Physics . gravity . y * gravityMultiplier * Time . deltaTime * gravityContributionMultiplier ; //Add gravity contribution
{
//Full speed ground movement
movementVector = inputVector * speed ;
//Resets the verticalMovement while on the ground,
//so that regardless of whether the player landed from a high fall or not,
//if they drop off a platform they will always start with the same verticalMovement.
gravityContributionMultiplier = 0f ;
}
}
// if player has to slide then add sideways speed to make it go down
if ( shouldSlide )
{
movementVector . x + = ( 1f - hitNormal . y ) * hitNormal . x * ( speed - slideFriction ) ;
movementVector . z + = ( 1f - hitNormal . y ) * hitNormal . z * ( speed - slideFriction ) ;
}
UpdateSlide ( ) ;
// check if the controller is grounded and above slope limit
// if player is grounded and above slope limit
// player has to slide
shouldSlide = ! ( Vector3 . Angle ( Vector3 . up , hitNormal ) < = characterController . slopeLimit ) ;
//Rotate to the movement direction
movementVector . y = 0f ;
if ( movementVector . sqrMagnitude > = . 0 2f )
float permittedDistance = characterController . radius / 2f ;
float topPositionY = transform . position . y + characterController . height ;
float distance = Mathf . Abs ( hit . point . y - topPositionY ) ;
public void Move ( Vector3 movement )
{
inputVector = movement ;
isJumping = false ; //This will stop the reduction to the gravity, which will then quickly pull down the character
}
private void UpdateSlide ( )
{
// if player has to slide then add sideways speed to make it go down
if ( shouldSlide )
{
movementVector . x + = ( 1f - hitNormal . y ) * hitNormal . x * ( speed - slideFriction ) ;
movementVector . z + = ( 1f - hitNormal . y ) * hitNormal . z * ( speed - slideFriction ) ;
}
// check if the controller is grounded and above slope limit
// if player is grounded and above slope limit
// player has to slide
currentSlope = Vector3 . Angle ( Vector3 . up , hitNormal ) ;
shouldSlide = currentSlope > = characterController . slopeLimit ;
}
}