如何防止游戏对象在碰撞时被相同的脚本破坏?
How to prevent the gameObjects being destroyed with the same script on Collision?
当玩家按下攻击其中一个游戏对象时,所有游戏对象(敌人)都会被摧毁。如果 gameObject(enemy) 与当前对象发生碰撞,我希望它能够销毁它,而其他对象不会被销毁。所有的敌人都有相同的剧本。
这是附加在 MainCharacter 游戏对象中的移动脚本..
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;
// }
//}
}
附在敌人身上的脚本。他们所有人都有敌人健康:1 统一输入。
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;
}
}
仅仅是因为函数 giveDamage
被声明为 static
(enemyHealth 成员也是如此)
Use the static modifier to declare a static member, which belongs to the type itself rather than to a specific object.
因此,每个敌人共享相同的生命值,当你伤害一个敌人时,实际上你伤害了所有敌人。
每个敌人都必须有自己的生命值。删除 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();
}
当玩家按下攻击其中一个游戏对象时,所有游戏对象(敌人)都会被摧毁。如果 gameObject(enemy) 与当前对象发生碰撞,我希望它能够销毁它,而其他对象不会被销毁。所有的敌人都有相同的剧本。
这是附加在 MainCharacter 游戏对象中的移动脚本..
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;
// }
//}
}
附在敌人身上的脚本。他们所有人都有敌人健康:1 统一输入。
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;
}
}
仅仅是因为函数 giveDamage
被声明为 static
(enemyHealth 成员也是如此)
Use the static modifier to declare a static member, which belongs to the type itself rather than to a specific object.
因此,每个敌人共享相同的生命值,当你伤害一个敌人时,实际上你伤害了所有敌人。
每个敌人都必须有自己的生命值。删除 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();
}