Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Raycast Ray Detection in Unity

2025-02-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

The purpose of this article is to share with you about how to use RaycastRays in Unity. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Project address: Raycast-SouthBegonia (https://github.com/SouthBegonia/UnityWorld/tree/master/Raycast)

Ray ray

Meaning: officially interpreted as an infinite line that starts at the origin point and faces the direction (however, according to the project verification, its default length is a unit vector, and the extended ray can be achieved only by multiplying the direction, not infinity).

Usage:

Ray ray = new Ray (transform.position,transform.forward)

Ray camerRay = Camera.main.ScreenPointToRay (Input.mousePosition)

RaycastHit Ray casting collision Information

Meaning: get the collision information obtained from the Raycast function (note that it is not collider ha, but contains collider information)

Key variables: point, collider, rigidbody, transform

Detection method-linetype detection of Physics.Raycast ray casting

Function: on the basis of having one ray (or not), use the ray (new ray) to carry out directional detection within a certain distance. The ray length can be modified, the Layer layer detected by the ray can be limited, and the collision information detected by the ray can be obtained. But only the first object hit by the ray can be detected, and the object behind can not be detected.

Usage:

Raycast (transform.position, Vector.forward, distance, LayerMask.GetMask ("Enemy"))

Raycast (transform.position, Vector.forward, distance, out RaycastHitInformation, LayerMask.GetMask ("Enemy"))

Raycast (MyRay, distance, LayerMash.GetMask ("Enemy"))

Raycast (MyRay, out RaycastHitInformation, distance, LayerMask.GetMask ("Enemy"))

Applicable situation: cooperate with camera coordinate transformation to realize all kinds of interaction

Physics.RaycastAll all Ray casting

Function: mechanism usage is roughly the same as Raycast, when the difference is that all objects on the ray path can be detected, return an array of detection information. The same is true for other methods with All suffixes

Usage:

RaycastHit [] hits = RaycastAll (Vector3.zero, Vector.forward, distance, LayerMask.GetMask ("Enemy"))

RaycastHit [] hits = RaycastAll (MyRay, distance, LayerMash.GetMask ("Enemy"))

Applicable occasions: penetration testing

Physics.Linecast segment projection

Function: establish the ray between two points to detect and return the bool type

Usage:

Linecast (startPos, endPos, LayerMask.GetMask ("Enemy"))

Linecast (startPos, endPos, out RaycastHit, LayerMask.GetMask ("Enemy"))

Applicable situation: local distance X-ray detection at a specific location

Detection method-somatotype detection of Physics.SphereCast sphere projection

Function: expand the detection range to spherical, return bool type. But the use of this projection needs to be very careful, see below

Usage:

SphereCast (originPos, radius, direction, out RaycastHit, distance, LayerMask.GetMask ("Enemy"))

SphereCast (Ray, radius, out RaycastHit, distance, LayerMask.GetMask ("Enemy"))

Applicable situation: detect whether the destination is reachable or not, so as to determine the mobility

Physics.Boxcast block projection / CapsuleCast capsule projection

Function: same sphere projection

Usage: same sphere projection

Physics.OverlapSphere intersecting sphere

Function: also detect sphere, but it is quite different from SphereCast

Usage:

Collider [] hits = Physics.OverlapSphere (startPos, radius, LayerMask.GetMask ("Enemy")): take startPos as the origin, create a sphere with a radius of radius, detect the whole spherical bounding body (solid), detect the objects on the Enemy layer, and return the collider of all colliding objects instead of RaycastHit

Application: a common method to detect whether there is a collision in the spherical range of a mounted object.

Physics.IgnoreCollision ignores collision IgnoreCollision (collider1, collider2, ignore)

Thank you for reading! This is the end of the article on "how to use Raycasting Ray Detection in Unity". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report