当前位置:网站首页>【Unity,C#】哨兵射线触发器模板代码
【Unity,C#】哨兵射线触发器模板代码
2022-08-01 16:02:00 【mozhimen】
哨兵射线触发器模板代码
效果
配置

代码
public class Observer : MonoBehaviour
{
public Transform player;
public GameEnding gameEnding;
private bool _isPlayerInRange = false;
private void OnTriggerEnter(Collider other)
{
if (other.transform == player)
{
_isPlayerInRange = true;
}
}
private void OnTriggerExit(Collider other)
{
if (other.transform == player)
{
_isPlayerInRange = false;
}
}
private void Update()
{
if (_isPlayerInRange)
{
//创建投射射线用到的方向矢量
Vector3 dirction = player.position - transform.position + Vector3.up;
//创建射线
Ray ray = new Ray(transform.position, dirction);
//射线集中对象,包含射线的碰撞信息
RaycastHit raycastHit;
//射线如果碰到物体
if (Physics.Raycast(ray, out raycastHit))
{
//如果碰到的是玩家
if (raycastHit.collider.transform == player)
{
gameEnding.fail();
}
}
}
}
}
边栏推荐
猜你喜欢

打破文件锁限制,以存储力量助力企业增长新动力

IronOS, an open source system for portable soldering irons, supports a variety of portable DC, QC, PD powered soldering irons, and supports all standard functions of smart soldering irons

LeetCode50天刷题计划(Day 6—— 整数反转 14.20-15.20)

“查找附近的商铺”|Geohash+MySQL实现地理位置筛选

VIM实用指南(0)基本概念与初次体验

【LeetCode】37、解数独

强网杯2022 pwn 赛题解析——yakagame

Inflation continues, Kenya's food security a concern

2.8K 120Hz touch dual-screen blessing Lingyao X dual-screen Pro 2022 makes the office without fear of imagination

指针进阶(三)之指针与数组笔试题
随机推荐
Go 单元测试
Spark: Cluster Computing with Working Sets
会议OA项目(六)--- (待开会议、历史会议、所有会议)
27英寸横置大屏+实体按键,全新探险者才是安全而合理的做法!
leetcode:80. 删除有序数组中的重复项 II
Grafana9.0发布,Prometheus和Loki查询生成器、全新导航、热图面板等新功能!
Row locks in MySQL
【无标题】
2022年7月最热的10篇AI论文
"Find nearby shops" | Geohash+MySQL realizes geographic location filtering
【Untitled】
uwsgi配置文件启动
27英寸横置大屏+实体按键,全新探险者才是安全而合理的做法
LeetCode50天刷题计划(Day 7—— 字符串转换整数 (atoi) 12.20-15.20)
what is tail tooth feast
蚂蚁首次披露核心基础软件技术开源版图
p5js炫酷网页流光动画
网站2D看板娘收集的可用的模型
js to determine whether it is a pc or a mobile terminal (including ipad)
LeetCode50天刷题计划(Day 6—— 整数反转 14.20-15.20)