Refine jump logic and add difference tracking
- Introduce `_shouldJump` and `_previousDifference` for smoother jump control. - Add `_initDifference` to reset difference tracking after each jump. - Refactor jump mechanics into a dedicated `Jump` method. - Adjust physics raycast distance for better collision detection. - Enhance `DebugUi` with updated difference and velocity tracking.
This commit is contained in:
@@ -12,6 +12,10 @@ public class TestScript : MonoBehaviour
|
||||
private ConfigurableJoint _configurableJoint;
|
||||
|
||||
private float _smoothX;
|
||||
|
||||
private bool _shouldJump = false;
|
||||
|
||||
private double? _previousDifference = null;
|
||||
|
||||
// max is 0.63
|
||||
// min is 0.15
|
||||
@@ -20,6 +24,7 @@ public class TestScript : MonoBehaviour
|
||||
private JointDrive _softJointDrive;
|
||||
|
||||
private double _maxDifference = 0;
|
||||
private double _initDifference = 0;
|
||||
|
||||
private bool _grounded = false;
|
||||
|
||||
@@ -68,6 +73,7 @@ public class TestScript : MonoBehaviour
|
||||
_jumpAction.Enable();
|
||||
|
||||
_maxDifference = GetDifference();
|
||||
_initDifference = _maxDifference;
|
||||
debugUi.UpdateText($"Difference: {_maxDifference}");
|
||||
|
||||
Time.timeScale = timeScale;
|
||||
@@ -98,28 +104,44 @@ public class TestScript : MonoBehaviour
|
||||
_smoothX = 0;
|
||||
}
|
||||
|
||||
if (_jumpAction.IsPressed() && _grounded) {
|
||||
if (_jumpAction.WasPressedThisFrame() && !_shouldJump) {
|
||||
_configurableJoint.yDrive = _softJointDrive;
|
||||
_shouldJump = true;
|
||||
}
|
||||
|
||||
if (_jumpAction.WasReleasedThisFrame() && _grounded) {
|
||||
float t = Mathf.InverseLerp(0.15f, 0.63f, (float)GetDifference()); // gives 0 when 0.63, 1 when 0.15
|
||||
float forceMultiplier = Mathf.Lerp(30, 5, t);
|
||||
|
||||
if (_shouldJump) {
|
||||
|
||||
debugUi.UpdateForceMultiplier(forceMultiplier.ToString());
|
||||
|
||||
pump.AddForce(transform.up * forceMultiplier, ForceMode.Impulse);
|
||||
_configurableJoint.yDrive = _jointDrive;
|
||||
}
|
||||
|
||||
if (GetDifference() < _maxDifference) {
|
||||
_maxDifference = GetDifference();
|
||||
if (GetDifference() < _maxDifference) {
|
||||
_maxDifference = GetDifference();
|
||||
}
|
||||
|
||||
_previousDifference ??= GetDifference();
|
||||
|
||||
if (_previousDifference < GetDifference()) {
|
||||
Jump();
|
||||
} else {
|
||||
_previousDifference = GetDifference();
|
||||
}
|
||||
}
|
||||
|
||||
debugUi.UpdateVelocity(_rigidbody.linearVelocity.y.ToString());
|
||||
debugUi.UpdateText($"Difference: {_maxDifference}");
|
||||
debugUi.UpdateCurrentText($"Current difference: {GetDifference()}");
|
||||
debugUi.UpdateGrounded(_grounded ? "Yes" : "No");
|
||||
debugUi.UpdateText($"{_maxDifference}");
|
||||
debugUi.UpdateCurrentText($"{GetDifference()}");
|
||||
}
|
||||
|
||||
private void Jump()
|
||||
{
|
||||
|
||||
float t = Mathf.InverseLerp(0.15f, 0.63f, (float)_maxDifference); // gives 0 when 0.63, 1 when 0.15
|
||||
float forceMultiplier = Mathf.Lerp(30, 5, t);
|
||||
|
||||
_shouldJump = false;
|
||||
_maxDifference = _initDifference;
|
||||
_previousDifference = null;
|
||||
|
||||
debugUi.UpdateForceMultiplier(forceMultiplier.ToString());
|
||||
|
||||
pump.AddForce(transform.up * forceMultiplier, ForceMode.Impulse);
|
||||
_configurableJoint.yDrive = _jointDrive;
|
||||
}
|
||||
|
||||
private void FixedUpdate()
|
||||
@@ -129,7 +151,7 @@ public class TestScript : MonoBehaviour
|
||||
|
||||
var origin = pump.position;
|
||||
var dir = -pump.transform.up; // world down
|
||||
const float distance = .35f;
|
||||
const float distance = 1f;
|
||||
|
||||
if (Physics.Raycast(origin, dir, out var hit, distance)) {
|
||||
Debug.DrawRay(origin, dir * hit.distance, Color.yellow);
|
||||
|
||||
Reference in New Issue
Block a user