【问题标题】:OnTriggerEnter2D not working for my enemy health systemOnTriggerEnter2D 不适用于我的敌人健康系统
【发布时间】:2023-01-15 02:31:03
【问题描述】:

我有一个射弹和一个敌人,但我希望敌人在接触射弹时减少健康变量。

我试着发射射弹,但它并没有降低生命值。

`使用系统集合; 使用 System.Collections.Generic; 使用统一引擎;

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);
        }
    }
}

【问题讨论】:

    标签: unity3d 2d projectile


    【解决方案1】:

    您可能遗漏了一件或多件事情:

    1. 在你的子弹和你的敌人中都有一个刚体。

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

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

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

      我希望我能帮到你!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-18
      • 1970-01-01
      • 2011-07-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多