Files
pogo-stick-game/Assets/TestScript.cs
ondroftw 021d01fbb1 Integrate Cinemachine and enhance debug capabilities
- Add Cinemachine package (v3.1.4) and configure related components (e.g., CinemachineBrain, CinemachineCamera, CinemachineFollow).
- Update scene with new Cinemachine virtual camera for improved camera behaviors and tracking.
- Extend `DebugUi` to display grounded status and added supporting logic in `TestScript`.
- Refine physics logic in `TestScript` with grounded checks and updated jump action conditions.
- Minor adjustments to Rigidbody angular damping and ForceMode values.
2025-10-13 18:49:31 +02:00

143 lines
4.0 KiB
C#

using System;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.Serialization;
public class TestScript : MonoBehaviour
{
private Rigidbody _rigidbody;
private InputAction _moveAction;
private InputAction _jumpAction;
private ConfigurableJoint _configurableJoint;
private float _smoothX;
// max is 0.63
// min is 0.15
private JointDrive _jointDrive;
private JointDrive _softJointDrive;
private double _maxDifference = 0;
private bool _grounded = false;
[SerializeField]
private float torqueForce;
[SerializeField]
private float lerpSpeed;
[SerializeField]
private float softPositionSpring = 200;
[SerializeField]
private ForceMode forceMode;
[SerializeField]
private Rigidbody pump;
[SerializeField]
private DebugUi debugUi;
[SerializeField]
private float timeScale = 1f;
private void Awake()
{
_rigidbody = GetComponent<Rigidbody>();
_configurableJoint = GetComponent<ConfigurableJoint>();
_jointDrive = _configurableJoint.yDrive;
_softJointDrive = new JointDrive {
positionSpring = softPositionSpring,
maximumForce = _jointDrive.maximumForce,
positionDamper = _jointDrive.positionDamper,
useAcceleration = _jointDrive.useAcceleration
};
}
private void Start()
{
_moveAction = InputSystem.actions.FindAction("Move");
_jumpAction = InputSystem.actions.FindAction("Jump");
_moveAction.Enable();
_jumpAction.Enable();
_maxDifference = GetDifference();
debugUi.UpdateText($"Difference: {_maxDifference}");
Time.timeScale = timeScale;
Debug.Log(transform.position);
}
private double GetDifference()
{
Vector3 axis = transform.up; // this transform's local Y in world space
Vector3 toOther = pump.transform.position - transform.position;
var val = Vector3.Dot(toOther, axis);
val = Mathf.Abs(val);
return Math.Round(val, 2); // signed distance along local Y
}
private void Update()
{
var moveValue = _moveAction.ReadValue<Vector2>();
if (moveValue.x != 0) {
_smoothX = Mathf.Lerp(_smoothX, moveValue.x, Time.deltaTime * lerpSpeed);
} else {
_smoothX = 0;
}
if (_jumpAction.IsPressed() && _grounded) {
_configurableJoint.yDrive = _softJointDrive;
}
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);
debugUi.UpdateForceMultiplier(forceMultiplier.ToString());
pump.AddForce(transform.up * forceMultiplier, ForceMode.Impulse);
_configurableJoint.yDrive = _jointDrive;
}
if (GetDifference() < _maxDifference) {
_maxDifference = GetDifference();
}
debugUi.UpdateVelocity(_rigidbody.linearVelocity.y.ToString());
debugUi.UpdateText($"Difference: {_maxDifference}");
debugUi.UpdateCurrentText($"Current difference: {GetDifference()}");
debugUi.UpdateGrounded(_grounded ? "Yes" : "No");
}
private void FixedUpdate()
{
// inverted to apply correct direction
_rigidbody.AddTorque(new Vector3(0, 0, -_smoothX) * torqueForce, forceMode);
var origin = pump.position;
var dir = -pump.transform.up; // world down
const float distance = .35f;
if (Physics.Raycast(origin, dir, out var hit, distance)) {
Debug.DrawRay(origin, dir * hit.distance, Color.yellow);
_grounded = true;
} else {
Debug.DrawRay(origin, dir * distance, Color.white);
_grounded = false;
}
}
}