// Update is called once per frame void FixedUpdate() { if (target == null || !AINavigator.CanReachTargetFrom(turret.muzzle.transform.position, target.gameObject)) { target = FindTarget(); } // Rotate if (target != null) { transform.rotation = Quaternion.RotateTowards(transform.rotation, Quaternion.LookRotation(target.transform.position - transform.position, new Vector3(0, 1, 0)), MaxRotation * Time.fixedDeltaTime); } }
// Update is called once per frame void FixedUpdate() { if ((FindObjectOfType <KillSoldiers> ().transform.position - transform.position).magnitude < killRadius) { GetComponent <HealthComponent>().Health -= 1; } animator.SetFloat("Speed", 1); if (reloadTimer > 0) { reloadTimer -= Time.fixedDeltaTime; } if (target == null || target.IsDead) { target = FindTarget(); } else { if (!AINavigator.CanReachTargetFrom(navigator.navTarget.position, target.gameObject)) { Vector2 sphere = Random.insideUnitCircle; Vector3 sphere2 = new Vector3(sphere.x, 0, sphere.y); navigator.navTarget.position = target.transform.position + sphere2 * distance; } } // Calculate turret's target var turretTarget = turret.GetVisualTarget(); if (turretTarget) { var h = turretTarget.GetComponent <HealthComponent> (); if (h == null) { h = turretTarget.GetComponentInParent <HealthComponent> (); } if (h && h.Team == EnemyTeam) { if (reloadTimer <= 0) { turret.Fire(); reloadTimer = ReloadTime; } } } }
public HealthComponent FindTarget() { float n = float.PositiveInfinity; HealthComponent bh = null; foreach (var h in FindObjectsOfType <HealthComponent>()) { if (h.Team == EnemyTeam && AINavigator.CanReachTargetFrom(turret.muzzle.transform.position, h.gameObject)) { float d = (h.transform.position - transform.position).magnitude; if (d < n) { bh = h; n = d; } } } return(bh); }