0
私は、ユニティを使用してマルチプレイヤーゲームを構築しています。メインプレイヤーは、ネットワークマネージャーを使用して生成されたプレハブで、プレーヤータグとが含まれています。以下は、のプレイヤータグを検索しているが、マルチプレイヤーではのプレイヤーを返す敵の攻撃スクリプトです.Gameobjectプレーヤーが配列でplayerHealthがPlayerHealthを取得できるように変更することはできますか?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);
}
}
}
複数のプレーヤーオブジェクトと複数のプレーヤーヘルス参照を保存しようとしていますか?あなたは 'FindGameObjectsWithTag()'を使って試したことがありますか?結果を反復処理しましたか? – Serlite
私は試してみましたが、役に立たなかった**エラー 'System.Array'に 'GetComponent'の定義が含まれていない** –
現在のスクリプトで複数のプレイヤー/ヘルスをどのように使用する予定ですか?それらをすべてコレクションに保存すれば、どのキャラクターが 'Attack()'の影響を受けているのかをどのように特定するのでしょうか?同時に敵は範囲内のすべての選手を攻撃することができますか?それとも1つを選択する必要がありますか?私は現在の状態であると感じています。この質問の範囲は、プレイヤーのコレクションを適切に取得して格納することだけではありません。後でゲームロジックを再作成する必要があります。 – Serlite