浏览代码

Merge pull request #26 from mwert09/master

Fixed steep surfaces bug and implemented sliding
/main
GitHub 4 年前
当前提交
f5ad616a
共有 1 个文件被更改,包括 31 次插入17 次删除
  1. 48
      UOP1_Project/Assets/Scripts/Characters/Character.cs

48
UOP1_Project/Assets/Scripts/Characters/Character.cs


[Tooltip("Represents how fast gravityContributionMultiplier will go back to 1f. The higher, the faster")] public float gravityComebackMultiplier = 15f;
[Tooltip("The maximum speed reached when falling (in units/frame)")] public float maxFallSpeed = 50f;
[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 gravityContributionMultiplier = 0f; //The factor which determines how much gravity is affecting verticalMovement
private bool isJumping = false; //If true, a jump is in effect and the player is holding the jump button

private float currentSlope;
private Vector3 hitNormal; // ground normal
private bool shouldSlide; // Should player slide?
private Vector3 inputVector; //Initial input horizontal movement (y == 0f)
private Vector3 movementVector; //Final movement vector

{
gravityContributionMultiplier += Time.deltaTime * gravityComebackMultiplier;
}
//Reduce the influence of the gravity while holding the Jump button
if (isJumping)
{

gravityContributionMultiplier *= gravityDivider; //Reduce the gravity effect
}
}
//Cap the maximum so the player doesn't reach incredible speeds when freefalling from high positions
//Cap the maximum so the player doesn't reach incredible speeds when freefalling from high positions
verticalMovement = Mathf.Clamp(verticalMovement, -maxFallSpeed, 100f);
}
else

//-5f is a good value to make it so the player also sticks to uneven terrain/bumps without floating.
//-5f is a good value to make it so the player also sticks to uneven terrain/bumps without floating
if (!isJumping)
{
verticalMovement = fallingVerticalMovement;

UpdateSlide();
//Rotate to the movement direction
movementVector.y = 0f;
if (movementVector.sqrMagnitude >= ROTATION_TRESHOLD)

private void OnControllerColliderHit(ControllerColliderHit hit)
{
hitNormal = hit.normal;
if (isMovingUpwards)
{
// Making sure the collision is near the top of the head

public void Move(Vector3 movement)
{
inputVector = movement;

{
if (characterController.isGrounded)
// Disable jumping if player has to slide
if (characterController.isGrounded && !shouldSlide)
{
isJumping = true;
jumpBeginTime = Time.time;

{
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
if (characterController.isGrounded)
{
currentSlope = Vector3.Angle(Vector3.up, hitNormal);
shouldSlide = currentSlope >= characterController.slopeLimit;
}
}
}
正在加载...
取消
保存