// Update is called once per frame void Update() { float lastDist = -1; Collider[] hitColliders = Physics.OverlapSphere(this.gameObject.transform.position, detectionRadius); if (currentTarget != null && (currentTarget.transform.position - this.transform.position).magnitude > AbandonRadius) { this.navMeshAgent.SetDestination(this.transform.position); this.currentTarget = null; } foreach (Collider collider in hitColliders) { Targetable tmp = collider.GetComponent <Targetable>(); if (tmp != null && tmp.GetSide() == toAttack && (lastDist < 0 || (this.transform.position - tmp.transform.position).magnitude < lastDist)) { currentTarget = tmp; lastDist = (this.transform.position - tmp.transform.position).magnitude; } } if (this.currentTarget != null && navMeshAgent != null) { navMeshAgent.SetDestination(this.currentTarget.transform.position); } else if (this.currentTarget == null && navMeshAgent != null && navMeshAgent.velocity.magnitude < 0.2) { Vector2 tmpWander = Random.insideUnitCircle * wander; Vector3 tmpWanderTwo = new Vector3(tmpWander.x, 0, tmpWander.y); navMeshAgent.SetDestination(tmpWanderTwo + this.transform.position); } }
private void OnTriggerEnter(Collider other) { Targetable target = other.GetComponent <Targetable>(); if (target != null && targetSide == target.GetSide()) { GetComponentInParent <TargetTracker>().AddTarget(target); } }
private void Update() { tracker.targets.Clear(); Collider[] close = Physics.OverlapSphere(transform.position, range, LayerMask.GetMask(targetSide == EntitySide.FRIENDLY ? "Minion" : "Enemy")); foreach (Collider col in close) { Targetable t = col.GetComponent <Targetable>(); if (t != null && t.GetSide() == targetSide) { tracker.targets.Add(t); } } }
Vector3 GetGroundRaycast(Vector3 onFail) { RaycastHit hit; Vector3 groundHit; groundHit = onFail; if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, Mathf.Infinity, LayerMask.GetMask("Ground"))) { Targetable target = hit.transform.GetComponent <Targetable>(); if (target != null) { if (target.GetSide() == EntitySide.ENNEMY) { groundHit = target.transform.position; } } else { groundHit = hit.point; } } lastRayHit = groundHit; return(groundHit); }