2017-02-12 2 views
0

화합을 사용하여 멀티 플레이어 게임을 제작 중입니다. 주요 플레이어는 네트워크 관리자를 사용하여 스폰되는 조립식이며 플레이어 태그와를 포함합니다. 아래는 태그를 검색했지만 멀티 플레이어에서는 플레이어을 반환하는 적이있는 적의 공격 스크립트입니다. GameObject 플레이어가 배열이고 playerHealth가 PlayerComponent를 얻을 수 있도록 내가 어떻게 바꿀 수 있습니까?FindGameObjecstWithTag를 사용하여 구성 요소를 가져 오는 방법>

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); 
     } 
    } 
} 
+0

여러 플레이어 개체 및 여러 플레이어 상태 참조를 저장하려고합니까? 당신은'FindGameObjectsWithTag()'를 사용해 보았고 그 결과를 반복 해 보았습니까? – Serlite

+0

시도했지만 도움이되지 않았다 ** 오류가 발생했습니다. ** 'System.Array'에 'GetComponent'에 대한 정의가 없습니다 ** –

+0

현재 스크립트에서 여러 플레이어/헬스를 어떻게 사용할 계획입니까? 컬렉션에 모든 것을 저장했다면, 어떤 것이 'Attack()'의 영향을 받는지 어떻게 식별 할 수 있습니까? 적군이 동시에 모든 범위의 플레이어를 공격 할 수 있습니까? 아니면 하나를 선택해야합니까? 나는 현재 상태에서 느낀다.이 질문의 범위는 플레이어 컬렉션을 올바르게 검색하고 저장하는 것 그 이상이다. 게임 논리에 대한 재 작업이 필요할 것이다. – Serlite

답변

0

당신은 invoke repeatingFindGameObjectsWithTag를 사용해야합니다. 테스트되지 않은 코드 스 니펫은 다음과 같습니다.

public GameObject [] playersList;

void Start() { 
      //Invokes the method methodName in time seconds, 
      //then repeatedly every repeatRate seconds. 
      InvokeRepeating("CheckPlayers", 2.0f, 0.3f); 
    } 


public void CheckPlayers(){ 
     //assign player in the player list 
     playersList= GameObject.FindGameObjectsWithTag ("Player"); 
     //now playersList contains all player, do what you want with it 

}