1
0
mirror of https://projects.caleb-brown.dev/UDRI-XRT/UDRIGEEKCup2024.git synced 2025-01-22 15:18:25 -05:00
UDRIGEEKCup2024/Assets/GolfControls/UI/ForceArrow.cs

108 lines
2.8 KiB
C#

using System;
using System.Collections;
using TMPro;
using UnityAtoms.BaseAtoms;
using UnityEngine;
namespace GolfControls
{
public class ForceArrow : MonoBehaviour
{
public FloatVariable AngleVariable;
public BoolVariable HideModelVariable;
public Transform followedBallTransform;
public GameObject Model;
public Vector3 arrowOffset = new Vector3(0, .5f, 0);
public float maxBounceSpeed = 0.07f;
public float bounceTimeSeconds = 0.5f;
private void Start()
{
StartCoroutine(Bouncing());
}
private void Update()
{
UpdatePos(followedBallTransform.position);
UpdateDirection(AngleVariable.Value);
Model.SetActive(!HideModelVariable.Value);
}
private IEnumerator Bouncing()
{
Transform child = Model.transform;
int dir = 1;
int startingDir = dir;
float timer = 0;
Vector3 startingPos = child.localPosition;
while (true)
{
while (true)
{
float percent = timer / bounceTimeSeconds;
Vector3 move = Vector3.up * (Time.deltaTime * percent * maxBounceSpeed * dir);
child.Translate(move,Space.World);
timer += Time.deltaTime;
if (timer >= bounceTimeSeconds)
{
timer = 0;
break;
}
yield return null;
}
while (true)
{
float percent = timer / bounceTimeSeconds;
percent = 1.0f - percent;
Vector3 move = Vector3.up * (Time.deltaTime * percent * maxBounceSpeed * dir);
child.Translate(move,Space.World);
timer += Time.deltaTime;
if (timer >= bounceTimeSeconds)
{
timer = 0;
dir = -dir;
if (dir == startingDir)
{
child.localPosition = startingPos;
}
break;
}
yield return null;
}
}
}
private void UpdateDirection(float angle)
{
Vector3 euler = new Vector3(0, angle, 0);
transform.localRotation = Quaternion.Euler(euler);
}
private void UpdatePos(Vector3 pos)
{
transform.position = pos + arrowOffset;
}
}
}