すべての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は統一で入力しました。機能giveDamage
をstatic
として宣言されているという理由だけ
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;
}
}
に従ってください! – Temmie