feat: Wall run
This commit is contained in:
@@ -6,6 +6,7 @@ public class Rocket : MonoBehaviour
|
||||
[System.Serializable]
|
||||
public class References
|
||||
{
|
||||
public Collider Collider;
|
||||
public Rigidbody Rigidbody;
|
||||
public ParticleSystem Fire;
|
||||
public GameObject Explosion;
|
||||
@@ -14,9 +15,11 @@ public class Rocket : MonoBehaviour
|
||||
[System.Serializable]
|
||||
public class Settings
|
||||
{
|
||||
public bool IsKinematic = false;
|
||||
public bool LaunchOnEnable = false;
|
||||
public float Speed = 20;
|
||||
public float Duration = 5;
|
||||
public float GostTime = 0;
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
@@ -30,13 +33,18 @@ public class Rocket : MonoBehaviour
|
||||
[SerializeField] private References _references;
|
||||
[SerializeField] private Settings _settings;
|
||||
[SerializeField, ReadOnly] private State _state;
|
||||
|
||||
private Vector3 _lastPos;
|
||||
|
||||
void OnEnable()
|
||||
{
|
||||
_references.Rigidbody.isKinematic = _settings.IsKinematic;
|
||||
|
||||
if (_settings.GostTime > 0)
|
||||
_references.Collider.enabled = false;
|
||||
|
||||
if (_settings.LaunchOnEnable)
|
||||
{
|
||||
Launch();
|
||||
}
|
||||
}
|
||||
|
||||
void Update()
|
||||
@@ -45,6 +53,7 @@ public class Rocket : MonoBehaviour
|
||||
{
|
||||
Vector3 force = transform.up * _settings.Speed * Time.deltaTime * 100;
|
||||
|
||||
_references.Collider.enabled = _state.FlightTime > _settings.GostTime;
|
||||
_references.Rigidbody.AddForce(force);
|
||||
|
||||
_state.FlightTime += Time.deltaTime;
|
||||
@@ -57,6 +66,27 @@ public class Rocket : MonoBehaviour
|
||||
}
|
||||
}
|
||||
|
||||
void LateUpdate()
|
||||
{
|
||||
if (_state.Launched && _state.FlightTime > _settings.Duration)
|
||||
{
|
||||
Vector3 currentPos = transform.position;
|
||||
Vector3 lookDir = currentPos - _lastPos;
|
||||
_lastPos = transform.position;
|
||||
|
||||
if (lookDir.sqrMagnitude > .001f)
|
||||
{
|
||||
lookDir = lookDir.normalized;
|
||||
|
||||
Quaternion targetRot = Quaternion.LookRotation(lookDir, Vector3.up);
|
||||
targetRot *= Quaternion.Euler(90, 0, 0);
|
||||
targetRot = Quaternion.Lerp(transform.rotation, targetRot, Time.deltaTime * 10);
|
||||
|
||||
_references.Rigidbody.MoveRotation(targetRot);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void OnCollisionEnter(Collision col)
|
||||
{
|
||||
if (_state.Launched && _state.FlightTime > .5f)
|
||||
@@ -71,10 +101,16 @@ public class Rocket : MonoBehaviour
|
||||
[ContextMenu("Launch")]
|
||||
public void Launch()
|
||||
{
|
||||
transform.parent = null;
|
||||
|
||||
_references.Rigidbody.isKinematic = false;
|
||||
|
||||
_state.Launched = true;
|
||||
_state.Disabled = false;
|
||||
_state.FlightTime = 0;
|
||||
|
||||
_references.Fire.Play();
|
||||
|
||||
_lastPos = transform.position;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user