触发对撞机的 2D 播放器损坏
2D player damage on trigger collider
所以我正在制作一个自上而下的小 space 射击游戏,到目前为止一切正常。我的问题是,当我更换武器时,我希望副武器比主武器造成更多伤害(可使用数字键更改)。这真是令人头疼,我们将不胜感激任何帮助。这是我试过的;
将武器添加到各个图层。在 OnTriggerEnter2D 函数中,我做了一个 if 语句,说明游戏对象(子弹)是否从第 13 层发射,带走 x 量的 HP。虽然什么都不做。下面是我当前的敌人代码。
using UnityEngine;
using System.Collections;
public class enemyDamage : MonoBehaviour {
public int health = 2;
void OnTriggerEnter2D(){
health--;
}
void Start () {
}
void Update () {
if (health <= 0){
Die();
}
}
void Die(){
Destroy (gameObject);
}
}
您可以通过向其添加 Collider2D
参数来更改 OnTriggerEnter2D
函数。这样您就可以访问触发该函数的游戏对象。
您没有 post 实例化子弹的代码。但是,如果您将项目符号名称设置为不同的值,则可以基于此降低生命值。
void OnTriggerEnter2D(Collider2D other) {
if(other.gameObject.name == "missile"){
health -= 10;
}else if(other.gameObject.name == "bullet"){
health -= 1;
}
}
或者更好的想法可能是在子弹中添加一个脚本,它知道子弹将造成的伤害量。
void OnTriggerEnter2D(Collider2D other) {
health -= other.gameObject.GetComponent<BulletScript>().damage;
}
所以我正在制作一个自上而下的小 space 射击游戏,到目前为止一切正常。我的问题是,当我更换武器时,我希望副武器比主武器造成更多伤害(可使用数字键更改)。这真是令人头疼,我们将不胜感激任何帮助。这是我试过的;
将武器添加到各个图层。在 OnTriggerEnter2D 函数中,我做了一个 if 语句,说明游戏对象(子弹)是否从第 13 层发射,带走 x 量的 HP。虽然什么都不做。下面是我当前的敌人代码。
using UnityEngine;
using System.Collections;
public class enemyDamage : MonoBehaviour {
public int health = 2;
void OnTriggerEnter2D(){
health--;
}
void Start () {
}
void Update () {
if (health <= 0){
Die();
}
}
void Die(){
Destroy (gameObject);
}
}
您可以通过向其添加 Collider2D
参数来更改 OnTriggerEnter2D
函数。这样您就可以访问触发该函数的游戏对象。
您没有 post 实例化子弹的代码。但是,如果您将项目符号名称设置为不同的值,则可以基于此降低生命值。
void OnTriggerEnter2D(Collider2D other) {
if(other.gameObject.name == "missile"){
health -= 10;
}else if(other.gameObject.name == "bullet"){
health -= 1;
}
}
或者更好的想法可能是在子弹中添加一个脚本,它知道子弹将造成的伤害量。
void OnTriggerEnter2D(Collider2D other) {
health -= other.gameObject.GetComponent<BulletScript>().damage;
}