using System; using System.Collections; using UnityAtoms.BaseAtoms; using UnityEngine; namespace GolfControls { public class BallHitController : MonoBehaviour { public VoidEvent ballHitEvent; public Vector3Variable AngleVariable; public FloatVariable PowerVariable; public BoolVariable isMovingVariable; public BoolVariable inHoleVariable; public float baseForce; private Rigidbody body; private void Start() { body = gameObject.GetComponent(); } private void OnEnable() { ballHitEvent.Register(OnHit); } private void OnDisable() { ballHitEvent.Unregister(OnHit); } private void OnHit() { if(isMovingVariable.Value) return; Vector3 hitVector = AngleVariable.Value.normalized * (PowerVariable.Value * baseForce); body.AddForce(hitVector); StartCoroutine(TrackState()); } private IEnumerator TrackState() { float minVelocityMagnitude = 0.1f; float ballStopTime = 1.0f; isMovingVariable.SetValue(true); while (!inHoleVariable.Value) { yield return null; if(body.velocity.magnitude >= minVelocityMagnitude) continue; float timer = 0.0f; bool isStopped = true; while (timer < ballStopTime) { yield return null; if (inHoleVariable.Value) break; timer += Time.deltaTime; if (body.velocity.magnitude > minVelocityMagnitude) { isStopped = false; break; } } if (isStopped) break; } body.velocity = Vector3.zero; body.angularVelocity = Vector3.zero; isMovingVariable.SetValue(false); } } }