Springboard & FallingPlatform

This commit is contained in:
2026-02-18 13:08:01 +01:00
parent f8d0d83476
commit b07c9a0d2a
9 changed files with 3367 additions and 5 deletions

View File

@@ -0,0 +1,90 @@
using System;
using System.Collections;
using UnityEngine;
public class FallingPlatform : MonoBehaviour
{
[System.Serializable]
private class Settings
{
public float FallDelay = .5f;
public float RecoverDelay = 5;
}
[System.Serializable]
private class References
{
public Rigidbody Rigidbody;
}
[SerializeField]
private Settings _settings;
[SerializeField]
private References _references;
int _originLayer;
Vector3 _originPosition;
Quaternion _originRotation;
private IEnumerator _fallCoroutine;
void OnTriggerEnter(Collider col)
{
if (Player.Owner && Player.Owner.gameObject == col.gameObject)
{
Fall();
}
}
void Awake()
{
_originLayer = _references.Rigidbody.gameObject.layer;
_originPosition = _references.Rigidbody.transform.position;
_originRotation = _references.Rigidbody.transform.rotation;
}
private void Fall()
{
if (_fallCoroutine != null)
return;
StartCoroutine(_fallCoroutine = FallCoroutine());
}
IEnumerator FallCoroutine()
{
yield return new WaitForSeconds(_settings.FallDelay);
Vector3 random = new Vector3(UnityEngine.Random.Range(-1, 1), UnityEngine.Random.Range(-1, 1), UnityEngine.Random.Range(-1, 1)).normalized;
_references.Rigidbody.isKinematic = false;
_references.Rigidbody.AddRelativeTorque(random, ForceMode.Impulse);
_references.Rigidbody.gameObject.layer = 0;
yield return new WaitForSeconds(_settings.RecoverDelay);
float duration = 1f;
_references.Rigidbody.isKinematic = true;
_references.Rigidbody.gameObject.layer = _originLayer;
Vector3 originPosition = _references.Rigidbody.transform.position;
Quaternion originRotation = _references.Rigidbody.transform.rotation;
for (float t = 0; t < duration; t += Time.deltaTime)
{
float normalizedTime = t / duration;
float easeTime = .5f - Mathf.Cos(normalizedTime * Mathf.PI) * .5f;
_references.Rigidbody.transform.position = Vector3.Lerp(originPosition, _originPosition, easeTime);
_references.Rigidbody.transform.rotation = Quaternion.Slerp(originRotation, _originRotation, easeTime);
yield return new WaitForEndOfFrame();
}
_references.Rigidbody.transform.position = _originPosition;
_references.Rigidbody.transform.rotation = _originRotation;
_fallCoroutine = null;
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: e4bf005511eccc940a19fd6bdbedc9bc

View File

@@ -588,8 +588,8 @@ public class Player : MonoBehaviour //NetworkBehaviour
: PlayerState.Idle;
}
if (_state.CurrentState != previousState)
Debug.Log($"{previousState} → {_state.CurrentState}");
//if (_state.CurrentState != previousState)
// Debug.Log($"{previousState} → {_state.CurrentState}");
}
#endregion
}

View File

@@ -0,0 +1,20 @@
using UnityEngine;
using UnityEngine.Events;
public class Springboard : MonoBehaviour
{
[SerializeField] private float _force = 60;
[SerializeField] private float _duration = .4f;
[SerializeField] private AnimationCurve _curve = AnimationCurve.EaseInOut(0, 1, 1, 0);
[SerializeField] private UnityEvent _onJump;
void OnTriggerEnter(Collider col)
{
if (Player.Owner && col.gameObject == Player.Owner.gameObject)
{
Debug.Log("Player triggered springboard");
Player.Owner.AddExtraForce(Vector3.up * _force, true, _duration, _curve);
_onJump?.Invoke();
}
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: effa7acb4388bcf4381294d9134d23f6