当前位置:网站首页>【Unity入门计划】基本概念-触发器 Trigger
【Unity入门计划】基本概念-触发器 Trigger
2022-07-25 07:26:00 【flashinggg】
目录
参考
碰撞器检测和触发器检测 - 简书 (jianshu.com)
在添加碰撞体组件时会有选择“是否是触发器”——Is Trigger的选项,勾选这个复选框后,刚体移动的时候就会穿过碰撞体。

在了解触发器之前,可以了解一下什么是触发事件。
触发事件
- 触发事件——当绑定了碰撞体的游戏对象进入到另一个游戏对象的触发范围内
- 触发事件发生时,不会发生直接的碰撞,而是——只要进入了触发器区域就会执行某些操作
触发器与碰撞体
碰撞体检测
Unity中碰撞体Coliider是一类组件,有很多种类。
碰撞检测的3种方法
- 两个游戏对象的碰撞器刚接触时(注意这里不是游戏对象接触,而是游戏对象的碰撞器Collider组件接触),调用一次OnCollisionEnter()函数
- 只要两个碰撞体一直在接触,就会一直调用OnCollisionStay()函数
- 两个碰撞体相互分离时,调用一次OnCollisionExit()函数
碰撞检测实现的条件
两个游戏对象A和B实现碰撞检测
- 运动的对象一定要是刚体,A和B至少有一个是刚体
- A和B必须都是碰撞体(Is Trigger必须都关闭,不能是触发器)
- A和B可以互相检测碰撞(都挂一个碰撞检测的script)
触发器与碰撞体的关系
- 在Unity中,检测碰撞发生的方式有两种:利用触发器检测&利用碰撞体检测
- 碰撞体可以看成是触发器的载体,触发器只是碰撞体的一个属性
触发检测
碰撞体的Is Trigger选项
勾选后,碰撞体就成了一个触发器,此时碰撞体的边界就成了触发器的区域。

当触发事件发生后,刚体不会与碰撞体物体发生直接的碰撞,而是直接穿透碰撞体。
触发检测的3种方法
- 如果碰撞体组件勾选了Is Trigger的选项,当触发事件发生时会调用OnTriggerEnter(Collier)函数
- 当仍在触发器范围内时,会调用OnTriggerStay(Collier)函数
- 当离开触发范围内时,会调用触发器的OnTriggerExit(Collier)函数
触发检测实现的条件
当A和B两个对象实现触发检测
- 运动的那个对象必须是刚体
- A和B只要有一个是触发器,就可以实现触发检测
- A和B可以互相实现触发检测(都挂一个触发检测的script)
触发器应用情景
当我们想要检测到物体之间的接触,又不想让碰撞检测影响物体的移动,这时候就可以用到触发器,比如下列情况就适合用触发器实现
- 检测物体是否经过地图上的某个区域
- 人靠近门的位置门自动打开
边栏推荐
- [pytorch] the most common function of view
- 分布式爬虫中的增量爬虫
- Luo min from qudian, prefabricate "leeks"?
- Default value of dart variable
- [programmer 2 Civil Servant] III. resource collection
- QT学习日记20——飞机大战项目
- Ideal L9, can't cross a pit on the road?
- [notes] search rotation sort array
- Servlet常用类剖析
- [semidrive source code analysis] [drive bringup] 39 - touch panel touch screen debugging
猜你喜欢

What if Oracle 19C migration encounters large lob tables?

Price reduction, game, bitterness, etc., vc/pe rushed to queue up and quit in 2022

LeetCode118. 杨辉三角

Leetcode skimming: dynamic programming 06 (integer splitting)

冰冰学习笔记:类与对象(上)

Simulation Implementation of list

BOM概述

Have you got the advanced usage of pytest?

QT6 with vs Code: compiling source code and basic configuration

Completely replace the redis+ database architecture, and JD 618 is stable!
随机推荐
关于GBase 自动关闭连接问题
Matlab self programming series (1) -- angular distribution function
DJI push code (one code for one use, limited time push)
钉钉最新版,怎么清除登录手机号历史记录数据
[computer explanation] what should I pay attention to when I go to the computer repair shop to repair the computer?
12 combination methods and risk interpretation of database architecture optimization (books available)
[semidrive source code analysis] [drive bringup] 38 - norflash & EMMC partition configuration
Practical skills -- some solutions to small problems
Meta is in a deep quagmire: advertisers reduce spending and withdraw from the platform
Ideal L9, can't cross a pit on the road?
Teach you to use cann to convert photos into cartoon style
Learn when playing No 7 | don't study this holiday, study only
Dart final and const variables
Luo min from qudian, prefabricate "leeks"?
第一启富金怎么样
【PyTorch】最常见的view的作用
【程序员2公务员】三、资源搜集
NLP hotspots from ACL 2022 onsite experience
J1 common DOS commands (P25)
Room database migration