1
0
mirror of https://projects.caleb-brown.dev/UDRI-XRT/UDRIGEEKCup2024.git synced 2025-01-22 07:08:51 -05:00
UDRIGEEKCup2024/Assets/Obstacles/SpearTrap/SpearTrap.cs
2024-04-21 22:15:04 -04:00

66 lines
2.2 KiB
C#

using System;
using System.Collections;
using UnityEngine;
namespace MAVRIC.GEEKCup.Obstacles.SpearTrap
{
public class SpearTrap : MonoBehaviour
{
public Transform trapTransform;
private Vector3 unTriggeredPosition;
private Vector3 triggeredPosition = Vector3.zero;
public float timeToTriggeredPositionSeconds = 0.05f;
public float trapForce = 100.0f;
private Vector3 globalTrapStartingPosition;
private void Start()
{
unTriggeredPosition = trapTransform.localPosition;
globalTrapStartingPosition = trapTransform.position;
}
public void TriggerTrap(float upTime)
{
StartCoroutine(MoveUp());
IEnumerator MoveUp()
{
for (float timer = 0; timer <= timeToTriggeredPositionSeconds; timer += Time.deltaTime)
{
yield return null;
float percent = timer / timeToTriggeredPositionSeconds;
Vector3 newPos = Vector3.Lerp(unTriggeredPosition, triggeredPosition, percent);
trapTransform.localPosition = newPos;
}
trapTransform.localPosition = triggeredPosition;
for (float timer = upTime; timer >= 0.0f; timer -= Time.deltaTime)
{
yield return null;
}
for (float timer = 0; timer <= timeToTriggeredPositionSeconds; timer += Time.deltaTime)
{
yield return null;
float percent = timer / timeToTriggeredPositionSeconds;
Vector3 newPos = Vector3.Lerp(triggeredPosition, unTriggeredPosition, percent);
trapTransform.localPosition = newPos;
}
trapTransform.localPosition = unTriggeredPosition;
}
}
private void OnTriggerEnter(Collider other)
{
if(other.attachedRigidbody == null) return;
other.attachedRigidbody.AddExplosionForce(trapForce,globalTrapStartingPosition,1.0f);
}
}
}