- Introduce `ZeroFriction` physics material for use in the scene. - Add `Pump` GameObject with various components, including Rigidbody, CapsuleCollider, and MeshRenderer. - Add configurable joint connecting `Upper` object and `Pump`. - Update `SampleScene` with new objects and configurations for physics interactions.
170 lines
4.5 KiB
C#
170 lines
4.5 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;
|
|
|
|
private bool _shouldJump = false;
|
|
|
|
private bool _chargedJump = false;
|
|
|
|
private double? _previousDifference = null;
|
|
|
|
// max is 0.63
|
|
// min is 0.15
|
|
|
|
private JointDrive _jointDrive;
|
|
private JointDrive _softJointDrive;
|
|
|
|
private double _maxDifference = 0;
|
|
private double _initDifference = 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;
|
|
|
|
[SerializeField]
|
|
private bool lerp = true;
|
|
|
|
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();
|
|
_initDifference = _maxDifference;
|
|
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 = lerp ? Mathf.Lerp(_smoothX, moveValue.x, Time.deltaTime * lerpSpeed) : moveValue.x;
|
|
} else {
|
|
_smoothX = 0;
|
|
}
|
|
|
|
if (_jumpAction.WasPressedThisFrame() && !_shouldJump) {
|
|
_configurableJoint.yDrive = _softJointDrive;
|
|
_shouldJump = true;
|
|
}
|
|
|
|
if (_shouldJump && _grounded) {
|
|
|
|
if (GetDifference() < _maxDifference) {
|
|
_maxDifference = GetDifference();
|
|
}
|
|
|
|
_previousDifference ??= GetDifference();
|
|
|
|
if (_previousDifference < GetDifference()) {
|
|
Jump();
|
|
} else {
|
|
_previousDifference = GetDifference();
|
|
}
|
|
}
|
|
|
|
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()
|
|
{
|
|
// 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 = .45f;
|
|
|
|
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;
|
|
}
|
|
}
|
|
}
|