unity3d OnTriggerEnter2D对我的敌人的健康系统不起作用

4sup72z8  于 2023-01-17  发布在  其他
关注(0)|答案(1)|浏览(96)

我有一个投射物和一个敌人,但是我希望敌人在接触投射物时减少健康变量。
我试着射击炮弹,但它没有减少健康。
'使用系统集合;使用系统.集合.泛型;使用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);
        }
    }
}
w1e3prcc

w1e3prcc1#

您可能错过了一个或多个内容:
1.一个刚体在你的子弹和你的敌人。
1.两个游戏对象内的碰撞器(非触发器)。
1.层碰撞矩阵必须在两个对象之间匹配(您可以在本文档中了解更多信息:https://docs.unity3d.com/Manual/LayerBasedCollision.html
1.如果子弹太快,你必须改变“碰撞检测模式”,同样,你可以在这里了解更多:https://docs.unity3d.com/ScriptReference/Rigidbody-collisionDetectionMode.html
希望我能帮到你!

相关问题