bool AcquireTarget() { ActionUnit closest = null; IOrderedEnumerable <ActionUnit> potentialTarget = ActionUnitManger.Instance.GetAll() .Where(x => x.UnitID != Host.UnitID && x.Group != Host.Group && x.Alive && x.BattleMode) .OrderBy(x => (Host.transform.position - x.transform.position).magnitude); NavMeshAgent nma = Host.GetComponent <NavMeshAgent>(); NavMeshObstacle nmo = Host.GetComponent <NavMeshObstacle>(); closest = potentialTarget.Where(x => _unreachable.IndexOf(x.UnitID) == -1).FirstOrDefault(); if (closest) { MarkTargetAttack(closest); return(true); } closest = potentialTarget.FirstOrDefault(); if (closest) { _unreachable.Clear(); //k tìm thấy mục tiêu thay thế thì giữ nguyên MarkTargetAttack(closest); return(true); } MarkTargetAttack(null); return(false); }
// Update is called once per frame void Update() { if (!Host.State.Equals(ActionUnit.UnitState.Move)) { return; } NavMeshAgent agent = Host.GetComponent <NavMeshAgent>(); //agent.destination = target; agent.isStopped = false; // TestCalculateSpeed(); agent.SetDestination(Host.TargetAttack.transform.position); // Host.GetComponent<BaseCharacterwNav>().SetTarget(Host.TargetAttack.transform); // Host.FaceTarget(); // Host.ChangeState(ActionUnit.UnitState.Move); // if (agent.velocity.sqrMagnitude > Mathf.Epsilon) // { // FaceTarget(Host.TargetAttack.transform.position); // // Host.transform.rotation = Quaternion.Lerp(Host.transform.rotation, Quaternion.LookRotation(agent.velocity.normalized), 0.1f); // } if (Host.IsEnemyInRangeAttack()) { //stop moving Debug.Log("Inside Range Attack"); agent.isStopped = true; // Host.GetComponent<BaseCharacterwNav>().SetTarget(null); Host.ChangeState(ActionUnit.UnitState.Attack); } }
public void Update() { _timeStuck += Time.deltaTime; if (_timeStuck >= 1f) { if (_lastPosition == null) { _lastPosition = _host.transform.position; _timeStuck = 0; } else { if ((_host.transform.position - _lastPosition).sqrMagnitude <= 1f) { Debug.Log("Stuck, change Target"); _host.GetComponent <ActionUnitFindTarget>().MarkCantReachTarget(_host.TargetAttack); } _lastPosition = _host.transform.position; _timeStuck = 0; } } Debug.DrawLine(_host.transform.position, _host.TargetAttack.transform.position, Color.green); }