当前位置:网站首页>[unity3d] emission (raycast) physical ray (Ray)
[unity3d] emission (raycast) physical ray (Ray)
2022-06-28 18:47:00 【little_ fat_ sheep】
1 Preface
Impactor assembly Collider This paper introduces the in 2 Collision detection between collision bodies , This paper will introduce the collision detection between physical ray and collision body . Physical rays are composed of Ray Definition , adopt Physics.RayCast / Physics.RayCastAll Emit radiation , return RayCastHit Collision detection information .
Rays are only used to detect collisions , The game interface cannot be seen , But users can use Debug.DrawRay Draw rays , adopt Debug.DrawLine Draw line segments , The rays and line segments they draw can only be in Scene Window view , stay Game The window is invisible .
1) Defining rays
// origin: The starting point , direction: Direction
public Ray(Vector3 origin, Vector3 direction)
// Screen ray : Start with the camera position , Rays projected to the direction of the mouse position on the near plane
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);2) Emit radiation
// ray: Rays to be emitted , hitInfo: Collision detection information , maxDistance: The farthest distance from which a ray is emitted , Returns whether a collision occurred , Only the first collision object is detected
public static bool Raycast(Ray ray)
public static bool Raycast(Ray ray, out RaycastHit hitInfo)
public static bool Raycast(Ray ray, float maxDistance)
public static bool Raycast(Ray ray, out RaycastHit hitInfo, float maxDistance)
// Check all collisions within the range of the ray
public static RaycastHit[] RaycastAll(Ray ray)
public static RaycastHit[] RaycastAll(Ray ray, float maxDistance)explain : The above method is Physics Class static methods ,hitInfo Encapsulates collision information , Contains the collision location point、 Colliding body collider etc. .
3) Commissioning ray
// start: Ray starting point , dir: The direction of the ray , color: Ray color
public static void DrawRay(Vector3 start, Vector3 dir)
public static void DrawRay(Vector3 start, Vector3 dir, Color color)
// start: The starting point of the line segment , end: End of line segment , color: Line color
public static void DrawLine(Vector3 start, Vector3 end)
public static void DrawLine(Vector3 start, Vector3 end, Color color)4) Area collision detection
// Check whether there is a collision body within the scope of the cube
public static bool CheckBox(Vector3 center, Vector3 halfExtents)
public static bool CheckBox(Vector3 center, Vector3 halfExtents, Quaternion orientation, int layermask)
// Check whether there is a collision body within the sphere
public static bool CheckSphere(Vector3 position, float radius)
public static bool CheckSphere(Vector3 position, float radius, int layerMask)
// Inspect whether there is collision body within the scope of capsule
public static bool CheckCapsule(Vector3 start, Vector3 end, float radius)
public static bool CheckCapsule(Vector3 start, Vector3 end, float radius, int layermask)explain : The above method is Physics Class static methods .
5) Project objects
// Project Cube
public static bool BoxCast(Vector3 center, Vector3 halfExtents, Vector3 direction)
public static bool BoxCast(Vector3 center, Vector3 halfExtents, Vector3 direction, out RaycastHit hitInfo, Quaternion orientation, float maxDistance, int layerMask)
public static RaycastHit[] BoxCastAll(Vector3 center, Vector3 halfExtents, Vector3 direction)
public static RaycastHit[] BoxCastAll(Vector3 center, Vector3 halfExtents, Vector3 direction, Quaternion orientation, float maxDistance, int layermask)
// Project a sphere
public static bool SphereCast(Ray ray, float radius)
public static bool SphereCast(Ray ray, float radius, out RaycastHit hitInfo, float maxDistance, int layerMask)
public static bool SphereCast(Vector3 origin, float radius, Vector3 direction, out RaycastHit hitInfo)
public static bool SphereCast(Vector3 origin, float radius, Vector3 direction, out RaycastHit hitInfo, float maxDistance, int layerMask)
public static RaycastHit[] SphereCastAll(Ray ray, float radius)
public static RaycastHit[] SphereCastAll(Ray ray, float radius, float maxDistance, int layerMask)
public static RaycastHit[] SphereCastAll(Vector3 origin, float radius, Vector3 direction)
public static RaycastHit[] SphereCastAll(Vector3 origin, float radius, Vector3 direction, float maxDistance, int layerMask)
// Project capsule
public static bool CapsuleCast(Vector3 point1, Vector3 point2, float radius, Vector3 direction)
public static bool CapsuleCast(Vector3 point1, Vector3 point2, float radius, Vector3 direction, out RaycastHit hitInfo, float maxDistance, int layerMask)
public static RaycastHit[] CapsuleCastAll(Vector3 point1, Vector3 point2, float radius, Vector3 direction)
public static RaycastHit[] CapsuleCastAll(Vector3 point1, Vector3 point2, float radius, Vector3 direction, float maxDistance, int layermask)explain : The above method is Physics Class static methods .
2 application
1) The game object
The object of the game Transform The component parameters are as follows :
| Name | Type | Position | Rotation | Scale | Color |
|---|---|---|---|---|---|
| Plane | Plane | (0, 0, 0) | (0, 0, 0) | (2, 2, 2) | #B8B2B2FF |
| Cube1 | Cube | (-3, 0.5, 3) | (0, 0, 0) | (1, 1, 1) | #F91D1DFF |
| Cube2 | Cube | (0, 0.5, 3) | (0, 0, 0) | (1, 1, 1) | #F91D1DFF |
| Cube3 | Cube | (3, 0.5, 3) | (0, 0, 0) | (1, 1, 1) | #F91D1DFF |
| Cube4 | Cube | (-3, 0.5, 0) | (0, 0, 0) | (1, 1, 1) | #F91D1DFF |
| Cube5 | Cube | (0, 0.5, 0) | (0, 0, 0) | (1, 1, 1) | #F91D1DFF |
| Cube6 | Cube | (3, 0.5, 0) | (0, 0, 0) | (1, 1, 1) | #F91D1DFF |
| Cube7 | Cube | (-3, 0.5, -3) | (0, 0, 0) | (1, 1, 1) | #F91D1DFF |
| Cube8 | Cube | (0, 0.5, -3) | (0, 0, 0) | (1, 1, 1) | #F91D1DFF |
| Cube9 | Cube | (3, 0.5, -3) | (0, 0, 0) | (1, 1, 1) | #F91D1DFF |
| Bullet | Sphere | (0, 0.5, -5) | (0, 0, 0) | (0.3, 0.3, 0.3) | #FDF723FF |
| BulletCreater | Empty | (0, 0, 0) | (0, 0, 0) | (1, 1, 1) | —— |
Add :Cube1~Cube9 and Bullet Add the Rigidbody Rigid body components , take Bullet Drag to Assets Window Resources/Prefabs Under the table of contents , Generate preset (prefab), And then delete Hierarchy Under the window Bullet object .

2) Script components
to BulletCreater Add game objects RayController Script components .
RayController.cs
using UnityEngine;
public class RayController : MonoBehaviour {
private GameObject bulletPrefab; // Shell preset
private RaycastHit hit; // Collision information
void Start () {
bulletPrefab = (GameObject) Resources.Load("Prefabs/Bullet");
hit = new RaycastHit();
}
void Update () {
if (Input.GetMouseButtonDown(0)) {
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit, 100)) {
// ray.origin = Camera.main.transform.position
GameObject bullet = Instantiate(bulletPrefab, ray.origin, Quaternion.identity); // Create shells from presets
bullet.GetComponent<Rigidbody>().velocity = ray.direction * 100;
Destroy(bullet, 3);
}
}
}
}3) Running effect

边栏推荐
- 基于固态激光雷达辅助的十六线机械雷达和单目相机的外参标定方法
- 推荐两款超高质量的壁纸软件
- io模型初探
- 华为云AOM发布2.0版本,3大特性亮相
- An in-depth analysis of the election mechanism in kubernetes
- leetcode 1647. Minimum Deletions to Make Character Frequencies Unique(所有字母频率不同的最小删除次数)
- 1 invalid import format(s) Postman Collection Format v1 is no longer supported and can not be import
- How to manage interface documents efficiently and gracefully
- 声网 VQA:将实时互动中未知的视频画质用户主观体验变可知
- leetcode 1689. Partitioning Into Minimum Number Of Deci-Binary Numbers(最少的“二进制数“个数)
猜你喜欢
随机推荐
启牛学堂的vip证券账户是真的安全正规吗?怎么说
Modular operation
Database comparison tool
Record an emotet Trojan horse handling case
MindSpore系列一加载图像分类数据集
ANR Application Not Responding
运筹学note
C语言指针的一些易错点
几行代码就能实现复杂的 Excel 导入导出,这个工具类真心强大!
Can I open an account today and buy shares today? Is it safe to open an account online?
使用.NetCore自带的后台作业,出入队简单模拟生产者消费者处理请求响应的数据
向上转型和向下转型
konva系列教程3:自定义图形
东方财富软件股票开户是靠谱的吗?在哪开户安全
ONEFLOW source code parsing: automatic inference of operator signature
匿名函数变量问题
业务层修改--根据现有框架的反推修改
3D可旋转粒子矩阵
PCB线路板布局和布线都有哪些设计要求?
CVPR2022 | 浙大、蚂蚁集团提出基于标签关系树的层级残差多粒度分类网络,建模多粒度标签间的层级知识









