Add ZeroFriction material and Pump object to scene
- 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.
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -15,6 +15,8 @@ public class TestScript : MonoBehaviour
|
|||||||
|
|
||||||
private bool _shouldJump = false;
|
private bool _shouldJump = false;
|
||||||
|
|
||||||
|
private bool _chargedJump = false;
|
||||||
|
|
||||||
private double? _previousDifference = null;
|
private double? _previousDifference = null;
|
||||||
|
|
||||||
// max is 0.63
|
// max is 0.63
|
||||||
|
|||||||
193
Assets/TestScript2.cs
Normal file
193
Assets/TestScript2.cs
Normal file
@@ -0,0 +1,193 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections;
|
||||||
|
using UnityEngine;
|
||||||
|
using UnityEngine.InputSystem;
|
||||||
|
using UnityEngine.Serialization;
|
||||||
|
|
||||||
|
public class TestScript2 : 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 double _initDifference = 0;
|
||||||
|
|
||||||
|
private bool _grounded = false;
|
||||||
|
private bool _jumpQueued = false;
|
||||||
|
private bool _upJumpQueued = false;
|
||||||
|
private bool _canJump = false;
|
||||||
|
private bool _jumpInputBuffer = false;
|
||||||
|
|
||||||
|
private double? _previousDifference = null;
|
||||||
|
|
||||||
|
[SerializeField]
|
||||||
|
private float torqueForce;
|
||||||
|
|
||||||
|
[SerializeField]
|
||||||
|
private float lerpSpeed;
|
||||||
|
|
||||||
|
[SerializeField]
|
||||||
|
private float jumpDownForce = 1f;
|
||||||
|
|
||||||
|
[SerializeField]
|
||||||
|
private float jumpUpForce = 1f;
|
||||||
|
|
||||||
|
[SerializeField]
|
||||||
|
private float softPositionSpring = 200;
|
||||||
|
|
||||||
|
[SerializeField]
|
||||||
|
private ForceMode forceMode = ForceMode.Impulse;
|
||||||
|
|
||||||
|
[SerializeField]
|
||||||
|
private Rigidbody pump;
|
||||||
|
|
||||||
|
[SerializeField]
|
||||||
|
private DebugUi debugUi;
|
||||||
|
|
||||||
|
[SerializeField]
|
||||||
|
private float timeScale = 1f;
|
||||||
|
|
||||||
|
[SerializeField]
|
||||||
|
private bool lerp = true;
|
||||||
|
|
||||||
|
private float _startAngularDamp;
|
||||||
|
|
||||||
|
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}");
|
||||||
|
|
||||||
|
_startAngularDamp = _rigidbody.angularDamping;
|
||||||
|
|
||||||
|
Time.timeScale = timeScale;
|
||||||
|
}
|
||||||
|
|
||||||
|
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 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);
|
||||||
|
|
||||||
|
_maxDifference = _initDifference;
|
||||||
|
_previousDifference = null;
|
||||||
|
_jumpQueued = false;
|
||||||
|
_canJump = false;
|
||||||
|
|
||||||
|
pump.AddForce(transform.up * (jumpUpForce * forceMultiplier), ForceMode.Impulse);
|
||||||
|
_configurableJoint.yDrive = _jointDrive;
|
||||||
|
}
|
||||||
|
|
||||||
|
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() && !_jumpInputBuffer) {
|
||||||
|
_jumpInputBuffer = true;
|
||||||
|
StartCoroutine(JumpInputBuffer());
|
||||||
|
}
|
||||||
|
|
||||||
|
if (_jumpInputBuffer && _grounded) {
|
||||||
|
_jumpInputBuffer = false;
|
||||||
|
_jumpQueued = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (_jumpQueued) {
|
||||||
|
|
||||||
|
if (GetDifference() < _maxDifference) {
|
||||||
|
_maxDifference = GetDifference();
|
||||||
|
}
|
||||||
|
|
||||||
|
_previousDifference ??= GetDifference();
|
||||||
|
|
||||||
|
if (_previousDifference < GetDifference() || _canJump) {
|
||||||
|
_canJump = true;
|
||||||
|
} else {
|
||||||
|
_previousDifference = GetDifference();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
IEnumerator JumpInputBuffer()
|
||||||
|
{
|
||||||
|
yield return new WaitForSeconds(0.2f);
|
||||||
|
_jumpInputBuffer = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void FixedUpdate()
|
||||||
|
{
|
||||||
|
// inverted to apply correct direction
|
||||||
|
_rigidbody.AddTorque(new Vector3(0, 0, -_smoothX) * torqueForce, forceMode);
|
||||||
|
|
||||||
|
if (_jumpQueued) {
|
||||||
|
_rigidbody.AddForce(-transform.up * jumpDownForce, ForceMode.Impulse);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (_canJump && _grounded) {
|
||||||
|
Jump();
|
||||||
|
}
|
||||||
|
|
||||||
|
var origin = pump.position;
|
||||||
|
var dir = -pump.transform.up; // world down
|
||||||
|
const float distance = .40f;
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
3
Assets/TestScript2.cs.meta
Normal file
3
Assets/TestScript2.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: f29f8671b3f4470296145841943e38a8
|
||||||
|
timeCreated: 1760541125
|
||||||
15
Assets/ZeroFriction.physicMaterial
Normal file
15
Assets/ZeroFriction.physicMaterial
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
%YAML 1.1
|
||||||
|
%TAG !u! tag:unity3d.com,2011:
|
||||||
|
--- !u!134 &13400000
|
||||||
|
PhysicsMaterial:
|
||||||
|
m_ObjectHideFlags: 0
|
||||||
|
m_CorrespondingSourceObject: {fileID: 0}
|
||||||
|
m_PrefabInstance: {fileID: 0}
|
||||||
|
m_PrefabAsset: {fileID: 0}
|
||||||
|
m_Name: ZeroFriction
|
||||||
|
serializedVersion: 2
|
||||||
|
m_DynamicFriction: 1
|
||||||
|
m_StaticFriction: 1
|
||||||
|
m_Bounciness: 0
|
||||||
|
m_FrictionCombine: 0
|
||||||
|
m_BounceCombine: 0
|
||||||
8
Assets/ZeroFriction.physicMaterial.meta
Normal file
8
Assets/ZeroFriction.physicMaterial.meta
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: e992aae4b1ad1f6448b54c4b9f54fd26
|
||||||
|
NativeFormatImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
mainObjectFileID: 13400000
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
Reference in New Issue
Block a user