diff --git a/Assets/Scripts/MovementHandler.cs b/Assets/Scripts/MovementHandler.cs index 733c058..925d710 100644 --- a/Assets/Scripts/MovementHandler.cs +++ b/Assets/Scripts/MovementHandler.cs @@ -1,21 +1,26 @@ +using System.Collections; using System.Collections.Generic; using System.Linq; +using UnityEditor.ProjectWindowCallback; using UnityEngine; using UnityEngine.AI; +using UnityEngine.InputSystem.iOS; [RequireComponent(typeof(Unit))] +[RequireComponent(typeof(Rigidbody))] public class MovementHandler : MonoBehaviour { [SerializeField] private float speed; [SerializeField] private NavMeshAgent agent; - [SerializeField] private Transform defaultMoveTarget; private Unit _unit; + private Rigidbody _rigidbody; void Awake() { _unit = GetComponent(); + _rigidbody = GetComponent(); } public void ChangeSpeed(float newSpeed) @@ -35,7 +40,7 @@ public class MovementHandler : MonoBehaviour public void MoveTowards(Vector3 destination) { - agent.SetDestination(destination); + if (agent.enabled) agent.SetDestination(destination); } public void MoveTowardsNearest() @@ -46,12 +51,20 @@ public class MovementHandler : MonoBehaviour Unit FindNearestEnemy() { List enemies = _unit.IsTeamA ? GlobalsVariable.AliveUnitsTeamB : GlobalsVariable.AliveUnitsTeamA; - return enemies - .Aggregate((nearest, current) => - (current.transform.position - transform.position).sqrMagnitude < - (nearest.transform.position - transform.position).sqrMagnitude - ? current - : nearest); + + Unit closestUnit = null; + float closestDistance = float.MaxValue; + foreach (Unit enemy in enemies) + { + float distanceToEnemy = (enemy.transform.position - transform.position).sqrMagnitude; + if (distanceToEnemy < closestDistance) + { + closestUnit = enemy; + closestDistance = distanceToEnemy; + } + } + + return closestUnit; } void Update() @@ -62,4 +75,30 @@ public class MovementHandler : MonoBehaviour MoveTowardsNearest(); } } + + public IEnumerator TakeImpulse(Vector3 impulse) + { + // Unity navmesh, can't handle physics (it rewrite velocity). So we deactivate it when applying force. + agent.enabled = false; + _rigidbody.AddForce(impulse, ForceMode.Impulse); + yield return new WaitForSeconds(0.5f); + + float noSurfaceTime = 0.0f; + + // Make sure to be on the navmesh surface before reactivating agent + while (_rigidbody != null && !NavMesh.SamplePosition(_rigidbody.position, out _, 1.0f, NavMesh.AllAreas)) + { + yield return new WaitForSeconds(0.5f); + noSurfaceTime += 0.5f; + + // Die if exited navMesh for to long + if (noSurfaceTime > 6.0f) + { + _unit.Health.Death(); + yield break; + } + + } + if (agent != null) agent.enabled = true; + } }