浏览代码

Fixed steep surfaces bug

/main
mwert09 4 年前
当前提交
e447593c
共有 1 个文件被更改,包括 30 次插入1 次删除
  1. 31
      UOP1_Project/Assets/Scripts/Characters/Character.cs

31
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 Vector3 hitNormal; // ground normal
private bool shouldSlide; // Should player slide?
//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)
{
//Less control in mid-air, conserving momentum from previous frame
movementVector = inputVector * speed;

}
}
// 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
shouldSlide = !(Vector3.Angle(Vector3.up, hitNormal) <= characterController.slopeLimit);
//Rotate to the movement direction
movementVector.y = 0f;

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

verticalMovement = 0f;
}
}
}
//---- COMMANDS ISSUED BY OTHER SCRIPTS ----

public void Jump()
{
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
}
}
正在加载...
取消
保存