2017-02-11 87 views
0

我正在構建一個使用團結的生存射擊遊戲資產的多人遊戲,玩家使用網絡管理員在場景中產生並具有標籤玩家。敵人由敵人管理器產生和管理,搜索玩家標籤和使敵人瞄準玩家,但敵人只攻擊第一代產生的玩家,而不會攻擊之後產生的玩家。如何讓所有玩家受到敵人的攻擊?

EnemyManager腳本

public class EnemyManager : MonoBehaviour 
{ 
    PlayerHealth playerHealth;  // Reference to the player's heatlh. 
    public GameObject enemy;    // The enemy prefab to be spawned. 
    public float spawnTime = 3f;   // How long between each spawn. 
    public Transform[] spawnPoints;   // An array of the spawn points this enemy can spawn from. 


    void Start() 
    { 
     // Call the Spawn function after a delay of the spawnTime and then continue to call after the same amount of time. 
     playerHealth = GameObject.FindWithTag("Player").GetComponent<PlayerHealth>(); 
     InvokeRepeating ("Spawn", spawnTime, spawnTime); 
    } 


    void Spawn() 
    { 
     // If the player has no health left... 
     if(playerHealth.currentHealth <= 0f) 
     { 
      // ... exit the function. 
      return; 
     } 

     // Find a random index between zero and one less than the number of spawn points. 
     int spawnPointIndex = Random.Range (0, spawnPoints.Length); 

     // Create an instance of the enemy prefab at the randomly selected spawn point's position and rotation. 
     Instantiate (enemy, spawnPoints[spawnPointIndex].position, spawnPoints[spawnPointIndex].rotation); 
    } 
} 

敵人的攻擊腳本

public class EnemyAttack : MonoBehaviour 
{ 
    public float timeBetweenAttacks = 0.5f;  // The time in seconds between each attack. 
    public int attackDamage = 10;    // The amount of health taken away per attack. 


    Animator anim;        // Reference to the animator component. 
    GameObject player;       // Reference to the player GameObject. 
    PlayerHealth playerHealth;     // Reference to the player's health. 
    EnemyHealth enemyHealth;     // Reference to this enemy's health. 
    bool playerInRange;       // Whether player is within the trigger collider and can be attacked. 
    float timer;        // Timer for counting up to the next attack. 


    void Awake() 
    { 
     // Setting up the references. 
     player = GameObject.FindGameObjectWithTag ("Player"); 
     playerHealth = player.GetComponent <PlayerHealth>(); 
     enemyHealth = GetComponent<EnemyHealth>(); 
     anim = GetComponent <Animator>(); 
    } 


    void OnTriggerEnter (Collider other) 
    { 
     // If the entering collider is the player... 
     if(other.gameObject == player) 
     { 
      // ... the player is in range. 
      playerInRange = true; 
     } 
    } 


    void OnTriggerExit (Collider other) 
    { 
     // If the exiting collider is the player... 
     if(other.gameObject == player) 
     { 
      // ... the player is no longer in range. 
      playerInRange = false; 
     } 
    } 


    void Update() 
    { 
     // Add the time since Update was last called to the timer. 
     timer += Time.deltaTime; 

     // If the timer exceeds the time between attacks, the player is in range and this enemy is alive... 
     if(timer >= timeBetweenAttacks && playerInRange && enemyHealth.currentHealth > 0) 
     { 
      // ... attack. 
      Attack(); 
     } 

     // If the player has zero or less health... 
     if(playerHealth.currentHealth <= 0) 
     { 
      // ... tell the animator the player is dead. 
      anim.SetTrigger ("PlayerDead"); 
     } 
    } 


    void Attack() 
    { 
     // Reset the timer. 
     timer = 0f; 

     // If the player has health to lose... 
     if(playerHealth.currentHealth > 0) 
     { 
      // ... damage the player. 
      playerHealth.TakeDamage (attackDamage); 
     } 
    } 
} 

敵人移動

public class EnemyMovement : MonoBehaviour 
{ 
    Transform player;    // Reference to the player's position. 
    PlayerHealth playerHealth;  // Reference to the player's health. 
    EnemyHealth enemyHealth;  // Reference to this enemy's health. 
    NavMeshAgent nav; 

    void Awake() 
    { 
     // Set up the references. 
     player = GameObject.FindGameObjectWithTag ("Player").transform; 
     playerHealth = player.GetComponent<PlayerHealth>(); 
     enemyHealth = GetComponent <EnemyHealth>(); 
     nav = GetComponent <NavMeshAgent>(); 
    } 


    void Update() 
    { 
     // If the enemy and the player have health left... 
     if(enemyHealth.currentHealth > 0 && playerHealth.currentHealth > 0) 
     { 
      // ... set the destination of the nav mesh agent to the player. 
      nav.SetDestination (player.position); 
     } 
     // Otherwise... 
     else 
     { 
      // ... disable the nav mesh agent. 
      nav.enabled = false; 
     } 
    } 
} 

本地播放器的安裝腳本

public class LocalPlayerSetup : NetworkBehaviour { 

    void Start() 
    { 
     GameObject.FindGameObjectWithTag ("EnemyManager").SetActiveRecursively (true); 

     if (isLocalPlayer) { 
      GameObject.FindGameObjectWithTag ("MainCamera").GetComponent<CameraFollow>().enabled = true; 
      GetComponent<PlayerMovement>().enabled = true; 
      GetComponentInChildren<PlayerShooting>().enabled = true; 
     } 

    } 

}

+0

您的代碼片段不會顯示腳本,顯示敵人如何決定要攻擊哪個玩家。但是,一般來說,敵人需要檢查所有玩家並決定攻擊哪一個,最常見的是選擇哪一個最接近。 – Abion47

+0

沒有敵人只是攻擊玩家第一次spawned.please檢查現在添加了敵人的攻擊和移動腳本 –

+0

目標是通過調用'GameObject.FindGameObjectWithTag'設置的,它只返回一個對象。你可以使用'GameObject.FindGameObjectsWithTag'獲得所有玩家,然後你可以遍歷它們來找到最接近的玩家。 (這假設所有的玩家實體都有''玩家''標籤。) – Abion47

回答

0

有幾個問題,你的代碼:

  1. 您在AWAKE事件尋找播放器(僅在 開始運行一次)
  2. 正在查找的玩家正在使用FindGameObjectWithTag其中只有 返回一個對象。
  3. 你在沒有Network Spawn的情況下會產生敵人,而不會在敵人身上產生敵人 。

通用解決您的複雜問題:

  1. 讓玩家的列表,並經常檢查使用調用重複播放器和計數。稍後,使用該列表(玩家列表)來攻擊玩家。
  2. 使用FindGameObjectsWithTag查找玩家,它會返回一個玩家列表。
  3. 使用Network Spawn在所有客戶身上產生敵人。
+0

我明白了你的觀點,但是我怎樣才能在我的代碼上實現它們? –