Unity 触发器案例

Unity 学习笔记汇总
Collider官方API使用文档

1. 前台

Cube的Is Trigger对应的复选框打上勾。
在这里插入图片描述

2. 代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NewBehaviourScript : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {

    }

    void OnTriggerEnter(Collider other)
    {
        Debug.Log("触发进入");
        Debug.Log(other.gameObject.name);
        Destroy(other.gameObject);  //销毁的是球
        Destroy(gameObject);    //销毁的是Cube
    }

    private void OnTriggerStay(Collider other)
    {
        Debug.Log("触发停留");
    }

    void OnTriggerExit(Collider other)
    {
        Debug.Log("触发退出");
    }
}

3. 结果

CubeSphere碰撞时,会打印相应的信息。
开始运行后,点击Scene,选中Sphere再按下W键,可以对Sphere进行移动。
在这里插入图片描述

发布了605 篇原创文章 · 获赞 637 · 访问量 140万+

猜你喜欢

转载自blog.csdn.net/COCO56/article/details/105238416