Unity:碰撞检测方法

背景:Unity游戏中碰撞检测有多种实现方法,有一些个人推荐记录一下。

具体方法

  1. OnCollider系列,虽然很常用,但个人并不是很推荐,因为这种方法需要获得Component的Collider,并不算最方便。
  2. CaracterController相关方法,只有进没有Exit,在不需要考虑Exit判定的情况下是简便好用的,但在触发对话这样要有Enter和Exit两个方法的场景就不太好用。
  3. ontrigger系列:这个目前是个人最推荐的,判定比较方便,方法也比较丰富,一般用Enter和Exit就可以了。
  4. 比如下面这段简单的代码,就可以方便地实现触发和关闭对话
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class showconv : MonoBehaviour
{
    
    
    public GameObject conv;
    public GameObject other;
    // Start is called before the first frame update
    private void OnTriggerEnter(Collider other)
    {
    
    
        if (other.gameObject.name == other.name) {
    
    
            conv.SetActive(true);
        }
    }
    private void OnTriggerExit(Collider other)
    {
    
    
        if (other.gameObject.name == other.name)
        {
    
    
            conv.SetActive(false);
        }
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_41697242/article/details/122104093