2016-10-20 17 views
0

すべてのgameObjects(敵)は、プレイヤーがいずれかのゲームオブジェクトに対する攻撃を押すと破壊されます。私はそれが現在のものと衝突した場合にgameObject(敵)を破壊し、他は破壊されないようにしたい。すべての敵は同じスクリプトを持っています。


はここ
Collisionで同じスクリプトでゲームオブジェクトが破壊されるのを防ぐには?

public class Movement : MonoBehaviour 
{ 
    public float movementSpeed = 6.0f; 
    public GameObject player; 
    public int jumpHeight = 350; 
    private bool onGround = false; 
    private bool afterMovingPlatform = false; 
    Animator anim; 

    //// Use this for initialization 
    void Start() 
    { 
     anim = GetComponent<Animator>(); 
     player = GetComponent<GameObject>(); 
    } 

    void Update() 
    { 
     //these are the codes for keyboard inputs when walking, attacking, etc... 
    } 

    void OnCollisionEnter2D(Collision2D coll) 
    { 
     if (coll.gameObject.tag == "Ground") 
     { 
      onGround = true; 

      if (afterMovingPlatform) 
      { 
       transform.parent = null; 
       afterMovingPlatform = false; 
      } 
     } 

     if ((coll.gameObject.tag == "Respawn" || coll.gameObject.tag == "Enemy") && (this.anim.GetCurrentAnimatorStateInfo(0).IsName("Attack Right") || this.anim.GetCurrentAnimatorStateInfo(0).IsName("Attack Left"))) 
     { 
      EnemyHealth.giveDamage(); 
     } 
     else if (coll.gameObject.tag == "Respawn" || coll.gameObject.tag == "Enemy") 
     { 
      CoinScore.score = 0; 
      Health.health = 3; 
      EnemyHealth.enemyHealth = 1; 
      SceneManager.LoadScene("LevelF"); 
      Debug.Log("RESPAWN"); 
     } 

     if (coll.gameObject.tag == "MovingPlatform") 
     { 
      onGround = true; 
      afterMovingPlatform = true; 
      transform.parent = coll.transform; 
     } 
    } 

    //void onCollisionExit2D(Collision2D coll) 
    //{ 
    // Debug.Log("EXIT"); 
    // if (coll.gameObject.tag == "MovingPlatform") 
    // { 
    //  transform.parent = null; 
    // } 
    //} 
} 


..敵に添付スクリプトそれはMainCharacterゲームオブジェクトに添付されたムーブメントスクリプトです。それらのすべてに敵の健康があります:1は統一で入力しました。機能giveDamagestaticとして宣言されているという理由だけ

using UnityEngine; 
using System.Collections; 

public class EnemyHealth : MonoBehaviour { 

    public static int enemyHealth = 1; 
    public Transform explosion; 
    public int enemyHealths; 
    CircleCollider2D coll; 

    // Use this for initialization 
    void Start() { 
     coll = GetComponent<CircleCollider2D>(); 
    } 

    // Update is called once per frame 
    void Update() 
    { 
     enemyHealths = enemyHealth; 
     if (enemyHealths <= 0) 
     { 
      Instantiate(explosion, transform.position, transform.rotation); 
      Destroy(coll.gameObject); 
     } 
    } 

    public static void giveDamage() 
    { 
     enemyHealth -= 1; 
    } 
} 

答えて

2

(SO enemyHealth部材がない)

型自体にではなくに属する静的メンバを宣言するためにstatic修飾子を使用し特定のオブジェクト。

このように、すべての敵は同じ健康状態を共有し、1つにダメージを与えると、実際にはすべての敵にダメージを与えます。

すべての敵は自分自身の健康を持っている必要があります。それは動作しますが、あなたに感謝staticキーワードを削除し、プレーヤーのスクリプトで

if ((coll.gameObject.tag == "Respawn" || coll.gameObject.tag == "Enemy") && (this.anim.GetCurrentAnimatorStateInfo(0).IsName("Attack Right") || this.anim.GetCurrentAnimatorStateInfo(0).IsName("Attack Left"))) 
{ 
    coll.gameObject.GetComponent<EnemyHealth>().giveDamage(); 
} 
+0

に従ってください! – Temmie

関連する問題