OnTriggerEnter2D对敌人的生命值系统不起作用



我有一个弹丸和一个敌人,但我希望敌人在接触弹丸时减少生命值。

我试着射击,但是没有减少生命值

使用System.Collections';使用System.Collections.Generic;使用UnityEngine;

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Health : MonoBehaviour
{
public int startHealth = 20;
public int health;
// Start is called before the first frame update
void Start()
{
health = startHealth;
}
// Update is called once per frame
void OnTriggerEnter2D(Collider2D col)
{
Debug.Log("Hit");
if (col.gameObject.tag == "PlayerProjectile")
{
health = health - 1;
}
}
void LateUpdate()
{
if (health < 1)
{
Destroy(gameObject);
}
}
}

你可能错过了一件或多件事:

  1. 子弹和敌人体内的刚体。

  2. 两个游戏对象中的碰撞器(非触发)。

  3. 两个对象之间的图层碰撞矩阵必须匹配(你可以在这个文档中了解更多:https://docs.unity3d.com/Manual/LayerBasedCollision.html)

  4. 如果子弹速度太快,必须更改"碰撞检测模式",也可以在这里了解更多信息:https://docs.unity3d.com/ScriptReference/Rigidbody-collisionDetectionMode.html

希望我能帮到你!

最新更新