Fix player jitter on moving platforms
This commit is contained in:
129
Assets/Content/Scripts/JetPack.cs
Normal file
129
Assets/Content/Scripts/JetPack.cs
Normal file
@@ -0,0 +1,129 @@
|
||||
using Unity.VisualScripting;
|
||||
using UnityEngine;
|
||||
using UnityEngine.InputSystem;
|
||||
|
||||
public class JetPack : MonoBehaviour
|
||||
{
|
||||
[System.Serializable]
|
||||
public class Settings
|
||||
{
|
||||
public float Force = 5;
|
||||
public float Duration = 10;
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
public class References
|
||||
{
|
||||
public BoxCollider Collider;
|
||||
public SphereCollider Trigger;
|
||||
public Rigidbody Rigidbody;
|
||||
public ParticleSystem Particles;
|
||||
public InputActionAsset InputActions;
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
public class StateContainer
|
||||
{
|
||||
public bool Equiped;
|
||||
}
|
||||
|
||||
[SerializeField]
|
||||
public Settings _settings;
|
||||
|
||||
[SerializeField]
|
||||
public References _references;
|
||||
|
||||
[SerializeField]
|
||||
public StateContainer _state;
|
||||
|
||||
private bool _wasPressed;
|
||||
private float _duration = 0;
|
||||
private float _smooth = 0;
|
||||
|
||||
private InputAction _jumpAction;
|
||||
|
||||
void OnTriggerEnter(Collider col)
|
||||
{
|
||||
if (Player.Instance && Player.Instance.gameObject == col.gameObject)
|
||||
{
|
||||
Equip();
|
||||
}
|
||||
}
|
||||
|
||||
void Awake()
|
||||
{
|
||||
_duration = _settings.Duration;
|
||||
_jumpAction = _references.InputActions.FindActionMap("Player").FindAction("Jump");
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
if (_state.Equiped && Player.Instance)
|
||||
{
|
||||
if (_duration > 0)
|
||||
{
|
||||
if (_jumpAction.IsPressed())
|
||||
{
|
||||
_wasPressed = true;
|
||||
_duration -= Time.deltaTime;
|
||||
_smooth = Mathf.Clamp01(_smooth + Time.deltaTime);
|
||||
//Player.Instance.SetExtraForce(Vector3.up * _settings.Force * _smooth, true);
|
||||
}
|
||||
else if (_wasPressed)
|
||||
{
|
||||
_smooth = 0;
|
||||
_wasPressed = false;
|
||||
ResetExtraForce();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Unequip();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void Equip()
|
||||
{
|
||||
if (_state.Equiped || _duration <= 0 ||!Player.Instance)
|
||||
return;
|
||||
|
||||
_wasPressed = false;
|
||||
_state.Equiped = true;
|
||||
_duration = _settings.Duration;
|
||||
|
||||
_references.Collider.enabled = false;
|
||||
_references.Trigger.enabled = false;
|
||||
_references.Rigidbody.isKinematic = true;
|
||||
|
||||
transform.parent = Player.Instance.transform.GetChild(0).GetChild(1).GetChild(0);
|
||||
transform.localPosition = new Vector3(0, .3f, -.4f);
|
||||
transform.localEulerAngles = Vector3.zero;
|
||||
}
|
||||
|
||||
private void Unequip()
|
||||
{
|
||||
if (!_state.Equiped)
|
||||
return;
|
||||
|
||||
ResetExtraForce();
|
||||
|
||||
_state.Equiped = false;
|
||||
_duration = _settings.Duration;
|
||||
|
||||
transform.parent = null;
|
||||
|
||||
Vector3 random = new Vector3(UnityEngine.Random.Range(-1, 1), UnityEngine.Random.Range(-1, 1), UnityEngine.Random.Range(-1, 1)).normalized;
|
||||
|
||||
_references.Collider.enabled = true;
|
||||
_references.Trigger.enabled = false;
|
||||
_references.Rigidbody.isKinematic = false;
|
||||
_references.Rigidbody.AddRelativeTorque(random, ForceMode.Impulse);
|
||||
}
|
||||
|
||||
|
||||
private void ResetExtraForce()
|
||||
{
|
||||
//Player.Instance.SetExtraForce(Vector3.zero, false);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user