I want to clamp the jump velocity to prevent player from flying very high when moving up on the ground, but the problem is, clamping the vertical velocity also means that I can't jump anymore while moving up, like you can see on the video. What can I do with it?
private void VariableJumpHeight(float dt)
{
if (Input.AReleased && !_released)
_released = true;
if (Actor.Flags.HasFlag(FlagType.OutOfControl)) return;
if (!Input.AHeld || _released) return;
if (!(_jumpTime < _config.jumpStartTime)) return;
Vector3 horizontal = Kinematics.HorizontalVelocity;
_jumpVelocity.y += _config.jumpHoldForce * dt;
float min = Mathf.Min(_jumpVelocity.y, _config.jumpMaxSpeed);
_jumpVelocity.y = min; // Clamping
Rigidbody.linearVelocity = horizontal + _jumpVelocity;
_jumpTime += dt;
}
private void ExecuteJump(bool bounce = false)
{
if (!bounce)
{
Vector3 horizontalVelocity = new Vector3(Rigidbody.linearVelocity.x, 0, Rigidbody.linearVelocity.z);
_jumpVelocity = Vector3.up * _config.jumpForce;
Rigidbody.linearVelocity = horizontalVelocity + _jumpVelocity;
}
if (bounce)
Rigidbody.linearVelocity = new Vector3(Rigidbody.linearVelocity.x, _config.jumpForce * 4, Rigidbody.linearVelocity.z);
}