added jumps
This commit is contained in:
1 parent
43ddb1b3b5
commit
ec8d233189
4 files changed
+61
-4
No files matched your search
@@ -8,12 +8,26 @@ namespace Code.Gameplay.Character.Controller
|
|||||||
private readonly float _maxStableMoveSpeed;
|
private readonly float _maxStableMoveSpeed;
|
||||||
private readonly float _sprintMoveSpeed;
|
private readonly float _sprintMoveSpeed;
|
||||||
private readonly float _stableMovementSharpness;
|
private readonly float _stableMovementSharpness;
|
||||||
|
private readonly float _jumpSpeed;
|
||||||
|
public bool JumpRequested { get; private set; }
|
||||||
|
|
||||||
public GroundMovementSystem(float maxStableMoveSpeed, float sprintMoveSpeed, float stableMovementSharpness)
|
public GroundMovementSystem(float maxStableMoveSpeed, float sprintMoveSpeed, float stableMovementSharpness, float jumpSpeed)
|
||||||
{
|
{
|
||||||
_maxStableMoveSpeed = maxStableMoveSpeed;
|
_maxStableMoveSpeed = maxStableMoveSpeed;
|
||||||
_sprintMoveSpeed = sprintMoveSpeed;
|
_sprintMoveSpeed = sprintMoveSpeed;
|
||||||
_stableMovementSharpness = stableMovementSharpness;
|
_stableMovementSharpness = stableMovementSharpness;
|
||||||
|
_jumpSpeed = jumpSpeed;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void RequestJump(bool jumpPressed)
|
||||||
|
{
|
||||||
|
if (jumpPressed)
|
||||||
|
JumpRequested = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void CancelJumpRequest()
|
||||||
|
{
|
||||||
|
JumpRequested = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void UpdateVelocity(
|
public void UpdateVelocity(
|
||||||
@@ -41,6 +55,18 @@ namespace Code.Gameplay.Character.Controller
|
|||||||
targetMovementVelocity,
|
targetMovementVelocity,
|
||||||
1f - Mathf.Exp(-_stableMovementSharpness * deltaTime)
|
1f - Mathf.Exp(-_stableMovementSharpness * deltaTime)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
if (JumpRequested)
|
||||||
|
{
|
||||||
|
currentVelocity += motor.CharacterUp * _jumpSpeed - Vector3.Project(currentVelocity, motor.CharacterUp);
|
||||||
|
JumpRequested = false;
|
||||||
|
motor.ForceUnground();
|
||||||
|
|
||||||
|
if (motor.CharacterController is CustomCharacterController controller)
|
||||||
|
{
|
||||||
|
controller.TriggerJump();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -33,6 +33,7 @@ namespace Code.Gameplay.Character.Core
|
|||||||
inputs.MoveAxisForward = moveInput.y;
|
inputs.MoveAxisForward = moveInput.y;
|
||||||
inputs.MoveAxisRight = moveInput.x;
|
inputs.MoveAxisRight = moveInput.x;
|
||||||
inputs.SprintPressed = _inputService.SprintHeld;
|
inputs.SprintPressed = _inputService.SprintHeld;
|
||||||
|
inputs.JumpPressed = _inputService.JumpPressed;
|
||||||
|
|
||||||
inputs.CameraRotation = FindFirstObjectByType<CameraController>().GetCameraRotation();
|
inputs.CameraRotation = FindFirstObjectByType<CameraController>().GetCameraRotation();
|
||||||
inputs.CameraPlanarDirection = FindFirstObjectByType<CameraController>().GetPlanarDirection();
|
inputs.CameraPlanarDirection = FindFirstObjectByType<CameraController>().GetPlanarDirection();
|
||||||
|
|||||||
@@ -17,8 +17,14 @@ namespace Code.Gameplay.Character.Controller
|
|||||||
[SerializeField] private float maxStableMoveSpeed;
|
[SerializeField] private float maxStableMoveSpeed;
|
||||||
[SerializeField] private float sprintMoveSpeed;
|
[SerializeField] private float sprintMoveSpeed;
|
||||||
[SerializeField] private float stableMovementSharpness;
|
[SerializeField] private float stableMovementSharpness;
|
||||||
|
[SerializeField] private float jumpSpeed;
|
||||||
|
[SerializeField] private float jumpCooldownDuration;
|
||||||
[SerializeField] private Vector3 gravity;
|
[SerializeField] private Vector3 gravity;
|
||||||
|
|
||||||
|
private float _jumpCooldownTimer;
|
||||||
|
private float JumpCooldownDuration => jumpCooldownDuration;
|
||||||
|
public bool CanJump => _jumpCooldownTimer <= 0f;
|
||||||
|
|
||||||
public void SetPosition(Vector3 position) => motor.SetPosition(position);
|
public void SetPosition(Vector3 position) => motor.SetPosition(position);
|
||||||
public void SetRotation(Quaternion rotation) => motor.SetRotation(rotation);
|
public void SetRotation(Quaternion rotation) => motor.SetRotation(rotation);
|
||||||
|
|
||||||
@@ -31,7 +37,8 @@ namespace Code.Gameplay.Character.Controller
|
|||||||
_groundMovement = new GroundMovementSystem(
|
_groundMovement = new GroundMovementSystem(
|
||||||
maxStableMoveSpeed,
|
maxStableMoveSpeed,
|
||||||
sprintMoveSpeed,
|
sprintMoveSpeed,
|
||||||
stableMovementSharpness
|
stableMovementSharpness,
|
||||||
|
jumpSpeed
|
||||||
);
|
);
|
||||||
_airMovement = new AirMovementSystem(
|
_airMovement = new AirMovementSystem(
|
||||||
gravity
|
gravity
|
||||||
@@ -44,6 +51,17 @@ namespace Code.Gameplay.Character.Controller
|
|||||||
public void SetInputs(ref CharacterInputs inputs)
|
public void SetInputs(ref CharacterInputs inputs)
|
||||||
{
|
{
|
||||||
_inputHandler.ProcessInputs(ref inputs, motor);
|
_inputHandler.ProcessInputs(ref inputs, motor);
|
||||||
|
|
||||||
|
var jumpInput = inputs.JumpPressed;
|
||||||
|
|
||||||
|
if (jumpInput && CanJump)
|
||||||
|
{
|
||||||
|
_groundMovement.RequestJump(true);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
_groundMovement.RequestJump(false);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void UpdateRotation(ref Quaternion currentRotation, float deltaTime)
|
public void UpdateRotation(ref Quaternion currentRotation, float deltaTime)
|
||||||
@@ -58,17 +76,27 @@ namespace Code.Gameplay.Character.Controller
|
|||||||
if (wasGrounded)
|
if (wasGrounded)
|
||||||
{
|
{
|
||||||
_groundMovement?.UpdateVelocity(ref currentVelocity, _inputHandler, motor, deltaTime);
|
_groundMovement?.UpdateVelocity(ref currentVelocity, _inputHandler, motor, deltaTime);
|
||||||
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
_airMovement?.UpdateVelocity(ref currentVelocity, deltaTime);
|
_airMovement?.UpdateVelocity(ref currentVelocity, deltaTime);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void TriggerJump()
|
||||||
|
{
|
||||||
|
_jumpCooldownTimer = JumpCooldownDuration;
|
||||||
|
}
|
||||||
|
|
||||||
public void BeforeCharacterUpdate(float deltaTime) { }
|
public void BeforeCharacterUpdate(float deltaTime) { }
|
||||||
public void PostGroundingUpdate(float deltaTime) { }
|
public void PostGroundingUpdate(float deltaTime) { }
|
||||||
public void AfterCharacterUpdate(float deltaTime) { }
|
|
||||||
|
public void AfterCharacterUpdate(float deltaTime)
|
||||||
|
{
|
||||||
|
_jumpCooldownTimer -= deltaTime;
|
||||||
|
if (_jumpCooldownTimer < 0f) _jumpCooldownTimer = 0f;
|
||||||
|
}
|
||||||
|
|
||||||
public bool IsColliderValidForCollisions(Collider coll) => true;
|
public bool IsColliderValidForCollisions(Collider coll) => true;
|
||||||
public void OnGroundHit(Collider hitCollider, Vector3 hitNormal, Vector3 hitPoint, ref HitStabilityReport hitStabilityReport) { }
|
public void OnGroundHit(Collider hitCollider, Vector3 hitNormal, Vector3 hitPoint, ref HitStabilityReport hitStabilityReport) { }
|
||||||
public void OnMovementHit(Collider hitCollider, Vector3 hitNormal, Vector3 hitPoint, ref HitStabilityReport hitStabilityReport) { }
|
public void OnMovementHit(Collider hitCollider, Vector3 hitNormal, Vector3 hitPoint, ref HitStabilityReport hitStabilityReport) { }
|
||||||
|
|||||||
@@ -539,6 +539,8 @@ MonoBehaviour:
|
|||||||
maxStableMoveSpeed: 6
|
maxStableMoveSpeed: 6
|
||||||
sprintMoveSpeed: 9
|
sprintMoveSpeed: 9
|
||||||
stableMovementSharpness: 50
|
stableMovementSharpness: 50
|
||||||
|
jumpSpeed: 10
|
||||||
|
jumpCooldownDuration: 0.5
|
||||||
gravity: {x: 0, y: -30, z: 0}
|
gravity: {x: 0, y: -30, z: 0}
|
||||||
--- !u!114 &1065569669
|
--- !u!114 &1065569669
|
||||||
MonoBehaviour:
|
MonoBehaviour:
|
||||||
|
|||||||
Reference in new issue
Block a user