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

边栏推荐
猜你喜欢
随机推荐
async-validator.js数据校验器
An in-depth analysis of the election mechanism in kubernetes
Lumiprobe非荧光炔烃研究丨DBCO NHS 酯
id门禁卡复制到手机_怎么把手机变成门禁卡 手机NFC复制门禁卡图文教程
618活动季——百数低代码平台特享折扣来临
select/poll/epoll
konva系列教程3:自定义图形
NFT流动性协议的安全困局—NFT借贷协议XCarnival被黑事件分析
使用Karmada实现Helm应用的跨集群部署
618 activity season - the arrival of special discounts for hundreds of low code platforms
Shanghai Pudong Development Bank Software Test interview real question
牛津大學教授Michael Wooldridge:AI社區近40年如何看待神經網絡
Graphic system - 2 View drawing
Professor Michael Wooldridge of Oxford University: how the AI community views neural networks in the past 40 years
打破学科之间壁垒的STEAM教育
第2章 处理文件、摄像头和图形用户界面cameo应用
C# 41. int与string互转
微软独家付费功能,也被完美解锁了
Alist+RaiDrive 给电脑整个80亿GB硬盘
About Statistical Distributions









