当前位置:网站首页>[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

边栏推荐
猜你喜欢

Professor Michael Wooldridge of Oxford University: how the AI community views neural networks in the past 40 years

使用Karmada实现Helm应用的跨集群部署

Qt 中 QObjectCleanupHandler 使用总结

用户网络模型与QoE

内存泄露

Win 10创建一个gin框架的项目

Graphic system - 1 Layout loading

1 invalid import format(s) Postman Collection Format v1 is no longer supported and can not be import

第2章 处理文件、摄像头和图形用户界面cameo应用

Chapter 2 processing files, cameras and GUI Cameo applications
随机推荐
19.2 container classification, array and vector container refinement
运筹学note
做跨境电商一定要学会用PRA软件,解放双手提高效率!
【Unity3D】相机跟随
Steam education to break the barriers between disciplines
新工作第一天
手动备份和还原DHCP服务器
Tensorboard Usage Summary
原生实现.NET5.0+ 自定义日志
618活动季——百数低代码平台特享折扣来临
19.2 容器分类、array、vector容器精解
刷题分析工具
Record an emotet Trojan horse handling case
Konva series tutorial 3: Customizing drawings
安装nodejs环境
Graphic system - 1 Layout loading
从理论到实践增强STEAM和工程教育
Win 10创建一个gin框架的项目
Qt 中 QObjectCleanupHandler 使用总结
180.1.连续登录N天(数据库)