// if we want to change the target strategy, it's here private void UpdateTarget() { GameObject[] enemyArray = GameObject.FindGameObjectsWithTag(enemyTag); float shortestDistance = Mathf.Infinity; GameObject nearestEnemy = null; // find the nearest enemy foreach (GameObject enemy in enemyArray) { // get ditance between the enemy and this float distanceToEnemy = Vector3.Distance(transform.position, enemy.transform.position); if (distanceToEnemy < shortestDistance) { shortestDistance = distanceToEnemy; nearestEnemy = enemy; } } if (nearestEnemy != null && shortestDistance <= range) { target = nearestEnemy.transform; targetEnemy = nearestEnemy.GetComponent <EnemyTuto>(); } else { target = null; if (isPlayingSound) { isPlayingSound = false; audioSource.Stop(); } } }
private void Awake() { if (Instance == null) { Instance = this; DontDestroyOnLoad(gameObject); } else { Destroy(gameObject); } }
// Use this for initialization void Start() { enemy = GetComponent <EnemyTuto>(); if (enemy.speed > 20 && enemy.speed <= 30) { delta = 0.6f; } else if (enemy.speed > 30) { delta = 1f; } targetWaypoint = Waypoints.pointArray[waypointIndex]; }