Springboard & FallingPlatform
This commit is contained in:
90
Assets/Content/Scripts/FallingPlatform.cs
Normal file
90
Assets/Content/Scripts/FallingPlatform.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
2
Assets/Content/Scripts/FallingPlatform.cs.meta
Normal file
2
Assets/Content/Scripts/FallingPlatform.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e4bf005511eccc940a19fd6bdbedc9bc
|
||||
@@ -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
|
||||
}
|
||||
20
Assets/Content/Scripts/Springboard.cs
Normal file
20
Assets/Content/Scripts/Springboard.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
2
Assets/Content/Scripts/Springboard.cs.meta
Normal file
2
Assets/Content/Scripts/Springboard.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: effa7acb4388bcf4381294d9134d23f6
|
||||
Reference in New Issue
Block a user