2017-02-11 6 views
0

私は団結のサバイバルシューター資産を使用してマルチプレイヤーゲームを構築しています、プレーヤーはプレハブシーンでネットワークマネージャを使用して起動され、タグプレーヤー【選択の敵は敵マネージャーによって生成され、管理されており、敵はプレイヤーをターゲットにしますが、敵は最初に生まれたプレイヤーだけを攻撃し、後で生まれたプレイヤーは攻撃しません。どのように敵をすべてのプレーヤーを攻撃するか? <strong>プレーヤー</strong>タグを検索

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

敵がプレイヤーを攻撃していません1st spawned.please checkに敵の攻撃と移動スクリプトが追加されました –

+0

ターゲットはGameObject.FindGameObjectWithTagを呼び出して設定されています。代わりに 'GameObject.FindGameObjectsWithTag'を使って*すべての*プレイヤーを取得することができます。そしてそれらを繰り返して、最も近いプレーヤーを見つけることができます。 (これは、すべてのプレーヤーエンティティが '' Player ''タグを持っていることを前提としています) – Abion47

答えて

0

あり、問題のカップルがあなたのコードである:あなたは(だけ 開始時に一度実行されます)AWAKEイベントで選手を見つけている

  1. あなたはFindGameObjectWithTagを使用しているプレイヤーを探していますが、 は1つのオブジェクトを返します。
  2. あなたはNetwork Spawnなしで敵を産んでいます。 は敵をクライアント上に生成しません。あなたの複雑な問題へ

一般的な解決策:

  1. プレーヤーのリストを作成し、頻繁に繰り返し呼び出しを使用してプレイヤーとそのカウントを確認してください。その後、そのリスト(プレイヤーリスト)を使用してプレイヤーを攻撃する。
  2. FindGameObjectsWithTagを使用してプレイヤーを検索すると、プレイヤーのリストが返されます。
  3. Network Spawnを使用すると、すべてのクライアントで敵を生成できます。
+0

私はあなたの意見を得ていますが、どうすれば自分のコードに実装できますか? –

関連する問題