Refactor jump mechanics and timer logic
- Replace `_upJumpQueued`, `_jumpInputBuffer`, and redundant joint drive logic with streamlined `_jumpTimer`, `_jumpTimerMilliseconds`, and `_jumpTimerFinished`. - Improve jump handling by introducing `_shouldJump` and `_canJump` with timer-based reset. - Remove unused joint configuration and redundant variables for cleaner implementation. - Update `SampleScene` with new jump parameters for better tuning.
This commit is contained in:
@@ -1070,11 +1070,11 @@ MonoBehaviour:
|
||||
m_Script: {fileID: 11500000, guid: f29f8671b3f4470296145841943e38a8, type: 3}
|
||||
m_Name:
|
||||
m_EditorClassIdentifier: Assembly-CSharp::TestScript2
|
||||
_jumpTimerMilliseconds: 450
|
||||
torqueForce: 8
|
||||
lerpSpeed: 7
|
||||
jumpDownForce: 3.5
|
||||
jumpUpForce: 3.5
|
||||
softPositionSpring: 200
|
||||
forceMode: 2
|
||||
pump: {fileID: 109005184}
|
||||
debugUi: {fileID: 375111999}
|
||||
|
||||
@@ -10,16 +10,11 @@ public class TestScript2 : MonoBehaviour
|
||||
private InputAction _moveAction;
|
||||
private InputAction _jumpAction;
|
||||
|
||||
private Action _jumpTimerFinished;
|
||||
|
||||
private ConfigurableJoint _configurableJoint;
|
||||
|
||||
private float _smoothX;
|
||||
private float _initDamping;
|
||||
|
||||
// max is 0.63
|
||||
// min is 0.15
|
||||
|
||||
private JointDrive _jointDrive;
|
||||
private JointDrive _softJointDrive;
|
||||
|
||||
private double _maxDifference = 0;
|
||||
private double _initDifference = 0;
|
||||
@@ -27,29 +22,30 @@ public class TestScript2 : MonoBehaviour
|
||||
private bool _grounded = false;
|
||||
private bool _jumpQueued = false;
|
||||
private bool _jumped = false;
|
||||
private bool _upJumpQueued = false;
|
||||
private bool _canJump = false;
|
||||
private bool _jumpInputBuffer = false;
|
||||
private bool _canJump = true;
|
||||
private bool _shouldJump = false;
|
||||
|
||||
private float _jumpTimer = 0f;
|
||||
|
||||
private double? _previousDifference = null;
|
||||
|
||||
[SerializeField]
|
||||
private float torqueForce;
|
||||
private float _jumpTimerMilliseconds = 450f;
|
||||
|
||||
[SerializeField]
|
||||
private float lerpSpeed;
|
||||
private float torqueForce = 8;
|
||||
|
||||
[SerializeField]
|
||||
private float jumpDownForce = 1f;
|
||||
private float lerpSpeed = 7;
|
||||
|
||||
[SerializeField]
|
||||
private float jumpUpForce = 1f;
|
||||
|
||||
private float jumpDownForce = 3.5f;
|
||||
|
||||
[SerializeField]
|
||||
private float softPositionSpring = 200;
|
||||
private float jumpUpForce = 3.5f;
|
||||
|
||||
[SerializeField]
|
||||
private ForceMode forceMode = ForceMode.Impulse;
|
||||
private ForceMode forceMode = ForceMode.VelocityChange;
|
||||
|
||||
[SerializeField]
|
||||
private Rigidbody pump;
|
||||
@@ -70,18 +66,6 @@ public class TestScript2 : MonoBehaviour
|
||||
private void Awake()
|
||||
{
|
||||
_rigidbody = GetComponent<Rigidbody>();
|
||||
_configurableJoint = GetComponent<ConfigurableJoint>();
|
||||
|
||||
_jointDrive = _configurableJoint.yDrive;
|
||||
|
||||
_initDamping = _rigidbody.angularDamping;
|
||||
|
||||
_softJointDrive = new JointDrive {
|
||||
positionSpring = softPositionSpring,
|
||||
maximumForce = _jointDrive.maximumForce,
|
||||
positionDamper = _jointDrive.positionDamper,
|
||||
useAcceleration = _jointDrive.useAcceleration
|
||||
};
|
||||
}
|
||||
|
||||
private void Start()
|
||||
@@ -96,11 +80,16 @@ public class TestScript2 : MonoBehaviour
|
||||
_initDifference = _maxDifference;
|
||||
debugUi?.UpdateText($"Difference: {_maxDifference}");
|
||||
|
||||
_initDamping = _rigidbody.angularDamping;
|
||||
_jumpTimerFinished += JumpTimerFinished;
|
||||
|
||||
Time.timeScale = timeScale;
|
||||
}
|
||||
|
||||
private void JumpTimerFinished()
|
||||
{
|
||||
_canJump = true;
|
||||
}
|
||||
|
||||
private double GetDifference()
|
||||
{
|
||||
Vector3 axis = transform.up; // this transform's local Y in world space
|
||||
@@ -122,15 +111,25 @@ public class TestScript2 : MonoBehaviour
|
||||
_maxDifference = _initDifference;
|
||||
_previousDifference = null;
|
||||
_jumpQueued = false;
|
||||
_canJump = false;
|
||||
_shouldJump = false;
|
||||
_jumped = true;
|
||||
_canJump = false;
|
||||
_jumpTimer = _jumpTimerMilliseconds / 1000;
|
||||
|
||||
pump.AddForce(transform.up * (jumpUpForce * forceMultiplier), ForceMode.Impulse);
|
||||
_configurableJoint.yDrive = _jointDrive;
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (_jumpTimer > 0) {
|
||||
_jumpTimer -= Time.deltaTime;
|
||||
|
||||
if (_jumpTimer <= 0) {
|
||||
_jumpTimer = 0;
|
||||
_jumpTimerFinished?.Invoke();
|
||||
}
|
||||
}
|
||||
|
||||
var moveValue = _moveAction.ReadValue<Vector2>();
|
||||
|
||||
if (moveValue.x != 0) {
|
||||
@@ -145,7 +144,7 @@ public class TestScript2 : MonoBehaviour
|
||||
_jumped = false;
|
||||
}
|
||||
|
||||
if (_jumpAction.IsPressed() && _grounded && !_jumped) {
|
||||
if (_jumpAction.IsPressed() && _grounded && !_jumped && _canJump) {
|
||||
_jumpQueued = true;
|
||||
}
|
||||
|
||||
@@ -157,8 +156,8 @@ public class TestScript2 : MonoBehaviour
|
||||
|
||||
_previousDifference ??= GetDifference();
|
||||
|
||||
if (_previousDifference < GetDifference() || _canJump) {
|
||||
_canJump = true;
|
||||
if (_previousDifference < GetDifference() || _shouldJump) {
|
||||
_shouldJump = true;
|
||||
} else {
|
||||
_previousDifference = GetDifference();
|
||||
}
|
||||
@@ -166,12 +165,6 @@ public class TestScript2 : MonoBehaviour
|
||||
}
|
||||
}
|
||||
|
||||
IEnumerator JumpInputBuffer()
|
||||
{
|
||||
yield return new WaitForSeconds(0.2f);
|
||||
_jumpInputBuffer = false;
|
||||
}
|
||||
|
||||
private void FixedUpdate()
|
||||
{
|
||||
// inverted to apply correct direction
|
||||
@@ -180,7 +173,7 @@ public class TestScript2 : MonoBehaviour
|
||||
if (_jumpQueued) {
|
||||
_rigidbody.AddForce(-transform.up * jumpDownForce, ForceMode.Impulse);
|
||||
|
||||
if (_canJump) {
|
||||
if (_shouldJump) {
|
||||
Jump();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user