当前位置:网站首页>Unity:利用 射线Ray 检测物体
Unity:利用 射线Ray 检测物体
2022-06-22 21:03:00 【还是要努力呀!】
利用 射线Ray 检测物体
Unity射线(Ray)是通过发射一条射线来检测碰撞体或触发器。不带碰撞器组件的物体时无法检测的,可以在物理设置里取消检测触发器(Edit → Project Setting → Physics/Physics2D)。
Physics.Raycast(origin, direction, out hitInfo, distance, mask)
origin:射线的发射位置;
direction:射线的发射方向。
hitInfo:射线击中的物体信息;
distance:射线距离,缺省表示无限距离;
mask:射线蒙版,表示检测了哪个layer,缺省表示检测所有层。此参数的形式有所要求:
方法一:
int shootableMask = LayerMask.GetMask("Shootable");
Physics2D.Raycast(Ray, out hitInfo, range, shootableMask);
方法二:
int layerNumber;
(1 << layerNumber) //只检测编号为 layerNumber 的层
~(1 << layerNumber) //不检测编号为 layerNumber 的层,其余的层都检测
(1 << layerNumber) | (1 << layer2Number) //只检测 2 个层
RaycastHit:存储发射射线后的碰撞信息
常用的成员变量:
collider:与射线发生碰撞的碰撞器;
transform:与射线发生碰撞的碰撞器的Transform;
distance:从射线起点到射线与碰撞器的交点的距离;
normal:射线射入平面的法向量;
point:射线与碰撞器交点的坐标。
常用用法
- 发射一条由摄像机向鼠标方向的一条射线;
- 获取该射线的碰撞信息;
- 获取碰撞的物体,对该物体进行操控。
代码实例
Ray myRay = Camera.main.ScreenPointToRay(Input.mousePosition);//从摄像机发出一条射线
RaycastHit2D hit = Physics2D.Raycast(new Vector2(myRay.origin.x, myRay.origin.y), Vector2.zero);//射线从鼠标点击屏幕的那个点出发,射到以当前点击位置为原点的坐标系中的垂直于(0,0)的位置,
if (hit.collider)//如果碰到有Collider2D组件的物体,找到该物体
{
Debug.Log(hit.transform.name);//打印出碰撞到的节点的名字
GameObject obj = GameObject.Find(hit.transform.name);
}
//通过 obj 来操作物体
边栏推荐
- Spark SQL 访问json和jdbc数据源
- js图片分辨率压缩
- From 11 hours to 25 seconds -- is there room for optimization?
- Introduction and example application of PostgreSQL string separator function (regexp\u split\u to\u table)
- The first warm-up -- implicit type conversion or other?
- Redis big key problem
- 2021-07-27
- SSH method 2 for adding node nodes in Jenkins
- Is it safe to open a securities account by downloading the qiniu app? Is there a risk?
- Remote access and control - SSH Remote Management and TCP wrappers access control
猜你喜欢

c语言---17 函数简介

优化——线性规划

C sqlsugar, hisql, FreeSQL ORM framework all-round performance test vs. sqlserver performance test

2021-04-14

2021-08-26

Digital data was invited to participate in Nantong enterprise digital transformation Seminar

2021-08-21

2021-04-14

Some shaders in AB package do not trigger the callback of ipreprocessshaders

wallys/WiFi6 MiniPCIe Module 2T2R 2 × 2.4GHz 2x5GHz
随机推荐
使用smart-doc自动生成接口文档
2021-08-22
Greedy interval problem (2)
Learn redis with you (11) -- redis distributed lock
Spark SQL Generic Load/Save Functions(2.4.3)
os.Args[1:]中命令行参数为空时,不执行内部语句
What if the SQL execution plan of the production system suddenly becomes worse?
Enjoy high-performance computing! Here comes the Tianyi cloud HPC solution
【22暑期复建1】 Codeforces Round #791 (Div. 2)
阻止别人使用浏览器调试
mysql主从同步及其分库分表基本流程
2021-04-16
A SQL optimization case using order by and rownum
Use the find command
为 localStorage 添加过期时间
Is it safe to make an appointment to pay new debts? Is it reliable?
2021-08-22
Relationship between adau1452 development system interface and code data
Considerations for using redisson to operate distributed queues
wallys/WiFi6 MiniPCIe Module 2T2R 2 × 2.4GHz 2x5GHz