[ad_1]
This can be a rigidbody-based motion script. I discover that it does not allow you to bounce whilst you’re operating. This downside persists whether or not you might be utilizing new Enter System or not (I examined each methods).
Notably, the bounce solely does not work whereas operating in case you are operating ahead (straight ahead, left-forward, or right-forward).
If you happen to strive leaping whereas operating backwards, left, or proper, the bounce will work. In case it issues, bounce is Area and run is Shift.
What is perhaps the rationale for this taking place?
utilizing System;
utilizing System.Collections.Generic;
utilizing UnityEngine;
public class HumanoidLandController: MonoBehaviour {
public Remodel CameraFollow;
[SerializeField] HumanoidLandInput _input;
[SerializeField] CameraController _cameraController;
[SerializeField] GameObject _playerObject; // Added for animations
Vector3 _playerObjectOriginalLocalPosition = Vector3.zero; // Added for animations
Rigidbody _rigidbody = null;
CapsuleCollider _capsuleCollider = null;
[SerializeField] Vector3 _playerMoveInput = Vector3.zero;
Vector3 _playerLookInput = Vector3.zero;
Vector3 _previousPlayerLookInput = Vector3.zero;
[SerializeField] float _cameraPitch = 0.0 f;
[SerializeField] float _playerLookInputLerpTime = 0.35 f;
[Header("Movement")]
[SerializeField] float _movementMultiplier = 30.0 f;
[SerializeField] float _notGroundedMovementMultiplier = 1.25 f;
[SerializeField] float _rotationSpeedMultiplier = 180.0 f;
[SerializeField] float _pitchSpeedMultiplier = 180.0 f;
[SerializeField] float _crouchSpeedMultiplier = 0.5 f;
[SerializeField] float _runMultiplier = 2.5 f;
[Header("Ground Check")]
[SerializeField][Range(0.0 f, 1.8 f)] float _groundCheckRadiusMultiplier = 0.9 f;
[SerializeField][Range(-0.95 f, 1.05 f)] float _groundCheckDistanceTolerance = 0.05 f;
[SerializeField] float _playerCenterToGroundDistance = 0.0 f;
public bool _playerIsGrounded {
get;
non-public set;
} = true; // Modified for animations
RaycastHit _groundCheckHit = new RaycastHit();
[Header("Gravity")]
[SerializeField] float _gravityFallCurrent = 0.0 f;
[SerializeField] float _gravityFallMin = 0.0 f;
[SerializeField] float _gravityFallIncrementTime = 0.05 f;
[SerializeField] float _playerFallTimer = 0.0 f;
[SerializeField] float _gravityGrounded = -1.0 f;
[SerializeField] float _maxSlopeAngle = 47.5 f;
[Header("Jumping")]
[SerializeField] float _initialJumpForceMultiplier = 750.0 f;
[SerializeField] float _continualJumpForceMultiplier = 0.1 f;
[SerializeField] float _jumpTime = 0.175 f;
[SerializeField] float _jumpTimeCounter = 0.0 f;
[SerializeField] float _coyoteTime = 0.15 f;
[SerializeField] float _coyoteTimeCounter = 0.0 f;
[SerializeField] float _jumpBufferTime = 0.2 f;
[SerializeField] float _jumpBufferTimeCounter = 0.0 f;
[SerializeField] bool _jumpWasPressedLastFrame = false;
public bool _playerIsJumping {
get;
non-public set;
} = false; // Modified for animations
non-public void Awake() {
_rigidbody = GetComponent < Rigidbody > ();
_capsuleCollider = GetComponent < CapsuleCollider > ();
_maxAscendRayDistance = _maxStepHeight / Mathf.Cos(_maximumAngleOfApproachToAscend * Mathf.Deg2Rad);
_maxDescendRayDistance = _maxStepHeight / Mathf.Cos(80.0 f * Mathf.Deg2Rad);
_numberOfStepDetectRays = Mathf.RoundToInt(((_maxStepHeight * 100.0 f) * 0.5 f) + 1.0 f);
_rayIncrementAmount = _maxStepHeight / _numberOfStepDetectRays;
_playerFullHeight = _capsuleCollider.top;
_playerCrouchedHeight = _playerFullHeight - _crouchAmount;
_playerObjectOriginalLocalPosition = _playerObject.rework.localPosition; // Added for animations
}
non-public void FixedUpdate() {
if (!_cameraController.UsingOrbitalCamera) {
_playerLookInput = GetLookInput();
PlayerLook();
PitchCamera();
}
_playerMoveInput = GetMoveInput();
PlayerVariables();
_playerIsGrounded = PlayerGroundCheck();
_playerMoveInput = PlayerMove();
_playerMoveInput = PlayerRun();
PlayerInfoCapture();
_playerMoveInput.y = PlayerFallGravity();
_playerMoveInput.y = PlayerJump();
Debug.DrawRay(_playerCenterPoint, _rigidbody.rework.TransformDirection(_playerMoveInput), Shade.purple, 0.5 f);
_playerMoveInput *= _rigidbody.mass; // NOTE: For dev functions.
_rigidbody.AddRelativeForce(_playerMoveInput, ForceMode.Pressure);
Debug.Log(_playerMoveInput.x);
}
non-public Vector3 GetLookInput() {
_previousPlayerLookInput = _playerLookInput;
_playerLookInput = new Vector3(_input.LookInput.x, (_input.InvertMouseY ? -_input.LookInput.y : _input.LookInput.y), 0.0 f);
return Vector3.Lerp(_previousPlayerLookInput, _playerLookInput * Time.deltaTime, _playerLookInputLerpTime);
}
non-public void PlayerLook() {
_rigidbody.rotation = Quaternion.Euler(0.0 f, _rigidbody.rotation.eulerAngles.y + (_playerLookInput.x * _rotationSpeedMultiplier), 0.0 f);
}
non-public void PitchCamera() {
_cameraPitch += _playerLookInput.y * _pitchSpeedMultiplier;
_cameraPitch = Mathf.Clamp(_cameraPitch, -89.9 f, 89.9 f);
CameraFollow.rotation = Quaternion.Euler(_cameraPitch, CameraFollow.rotation.eulerAngles.y, CameraFollow.rotation.eulerAngles.z);
}
non-public Vector3 GetMoveInput() {
return new Vector3(_input.MoveInput.x, 0.0 f, _input.MoveInput.y);
}
non-public void PlayerVariables() {
_playerCenterPoint = _rigidbody.place + _capsuleCollider.heart;
}
non-public Vector3 PlayerMove() {
return ((_playerIsGrounded) ? (_playerMoveInput * _movementMultiplier) : (_playerMoveInput * _movementMultiplier * _notGroundedMovementMultiplier));
}
non-public bool PlayerGroundCheck() {
float sphereCastRadius = _capsuleCollider.radius * _groundCheckRadiusMultiplier;
Physics.SphereCast(_playerCenterPoint, sphereCastRadius, Vector3.down, out _groundCheckHit);
_playerCenterToGroundDistance = _groundCheckHit.distance + sphereCastRadius;
return ((_playerCenterToGroundDistance >= _capsuleCollider.bounds.extents.y - _groundCheckDistanceTolerance) &&
(_playerCenterToGroundDistance <= _capsuleCollider.bounds.extents.y + _groundCheckDistanceTolerance));
}
non-public Vector3 PlayerRun() {
Vector3 calculatedPlayerRunSpeed = _playerMoveInput;
if (_input.MoveIsPressed && _input.RunIsPressed && !_playerIsCrouching) {
calculatedPlayerRunSpeed *= _runMultiplier;
}
return calculatedPlayerRunSpeed;
}
non-public void PlayerInfoCapture() {
if (_playerIsGrounded && _groundCheckHit.collider) {
_lastGroundCheckHit = _groundCheckHit;
_playerMoveInputAtLastGroundCheckHit = _playerMoveInput;
}
}
non-public float PlayerFallGravity() {
float gravity = _playerMoveInput.y;
if (_playerIsGrounded || _playerIsAscendingStairs || _playerIsDescendingStairs) {
_gravityFallCurrent = _gravityFallMin; // Reset
} else {
_playerFallTimer -= Time.fixedDeltaTime;
if (_playerFallTimer < 0.0 f) {
float gravityFallMax = _movementMultiplier * _runMultiplier * 5.0 f;
float gravityFallIncrementAmount = (gravityFallMax - _gravityFallMin) * 0.1 f;
if (_gravityFallCurrent < gravityFallMax) {
_gravityFallCurrent += gravityFallIncrementAmount;
}
_playerFallTimer = _gravityFallIncrementTime;
}
gravity = -_gravityFallCurrent;
}
return gravity;
}
non-public float PlayerJump() {
float calculatedJumpInput = _playerMoveInput.y;
SetJumpTimeCounter();
SetCoyoteTimeCounter();
SetJumpBufferTimeCounter();
if (_jumpBufferTimeCounter > 0.0 f && !_playerIsJumping && _coyoteTimeCounter > 0.0 f) {
//if (Vector3.Angle(_rigidbody.rework.up, _groundCheckHit.regular) < _maxSlopeAngle)
//{
calculatedJumpInput = _initialJumpForceMultiplier;
_playerIsJumping = true;
_jumpBufferTimeCounter = 0.0 f;
_coyoteTimeCounter = 0.0 f;
//}
} else if (_input.JumpIsPressed && _playerIsJumping && !_playerIsGrounded && _jumpTimeCounter > 0.0 f) {
calculatedJumpInput = _initialJumpForceMultiplier * _continualJumpForceMultiplier;
} else if (_playerIsJumping && _playerIsGrounded) {
_playerIsJumping = false;
}
return calculatedJumpInput;
}
non-public void SetJumpTimeCounter() {
if (_playerIsJumping && !_playerIsGrounded) {
_jumpTimeCounter -= Time.fixedDeltaTime;
} else {
_jumpTimeCounter = _jumpTime;
}
}
non-public void SetCoyoteTimeCounter() {
if (_playerIsGrounded) {
_coyoteTimeCounter = _coyoteTime;
} else {
_coyoteTimeCounter -= Time.fixedDeltaTime;
}
}
non-public void SetJumpBufferTimeCounter() {
if (!_jumpWasPressedLastFrame && _input.JumpIsPressed) {
_jumpBufferTimeCounter = _jumpBufferTime;
} else if (_jumpBufferTimeCounter > 0.0 f) {
_jumpBufferTimeCounter -= Time.fixedDeltaTime;
}
_jumpWasPressedLastFrame = _input.JumpIsPressed;
}
}
```
[ad_2]