You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity中玩家GameObject无法检测投射物碰撞问题求助

碰撞检测失效问题

我有玩家和AI敌人对象,敌人进入玩家范围时会生成投射物。挂载在玩家身上的代码原本要检测带“Axe”标签的投射物碰撞并减少生命值,但玩家完全检测不到任何碰撞——就连if语句外的日志都没输出。

玩家挂载的代码

public class bullet_hit : MonoBehaviour
{
    public int  maxHealth = 10;
    public int currentHealth;
    public HealthBar HealthBar;
      
    // Start is called before the first frame update
    public void Start()
    {
        currentHealth = maxHealth;
        HealthBar.SetMaxHealth(maxHealth);
    }

    // Update is called once per frame
    public void Update()
    { 
        void OnCollisionEnter(Collision other)
        {
            Debug.Log("hit by" + other.gameObject.name);
         
            if (other.gameObject.tag == "Axe")
            {
                Debug.Log("hit by ball");
                TakeDamage(2);    
            }
        }
 
        void TakeDamage (int damage)
        {
            Debug.Log("took damage");
            currentHealth -= damage;
            HealthBar.SetHealth(currentHealth);
        }
    }
}

已完成的排查操作

  • 确认投射物的“Axe”标签设置正确
  • 玩家与投射物均已添加Collider和Rigidbody组件
  • 检查项目设置中的Layer碰撞矩阵,所有碰撞选项已勾选
  • 控制台无任何报错信息

相关组件截图

  • 玩家Rigidbody:Player Rigid Body
  • 玩家Collider:Player Collider
  • 投射物Rigidbody和Collider:Ball rigid body and collider

内容的提问来源于stack exchange,提问作者Coding_Goose

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 13:45:23