This is the 13th day of my participation in the August More Text Challenge. For details, see:August is more challenging
An overview of the
Radiography is one of the most common features in Unity, and it’s very important. Ray detection in Unity has a wide range of applications, mostly used for collision detection, such as: in the game, when shooting, need to determine the hit object, the bullet hit position, and sometimes use the mouse to control the movement of the object or the mouse to pick up an object, etc., all these need Physics, Raycast and Ray. That’s how important they are. Let’s take a look at how radiography is used in Unity.
ray
A ray, as the name suggests, in mathematics is a straight line formed by one end of a segment extending indefinitely. A ray has only one end and cannot be measured (it is infinitely long). In Unity, radiography emits a ray from a fixed point in a specific direction and returns collision information when it collids with an object.
Detailed explanation and usage
In the official API, we will find that Ray is a structure ()
Let’s look at ray in action
public Ray(Vector3 origin, Vector3 direction);
Copy the code
Origin
: The starting point of the rayDirection
: The direction of the ray
That’s part of the official API, but Ray can’t do just that. There are many other applications, such as shooting rays directly from the camera, radiographic detection returning collision information, and so on.
Ray Camera.main.ScreenPointToRay(Vector3 pos)
Returns a Ray Ray from the camera to a specified point on the screenRay Camera.main.ViewportPointToRay(Vector3 pos)
Returns a Ray Ray specifying a point from the camera to the viewport (not outside the viewport)RaycastHit
Radiographic detection returns collision information about raysbool Physics.Raycast
Whether the ray has collided with the collider in which the object occurs, the collision istrue
RaycastHit[] RaycastAll
Radiographic detection and return all collisions, so radiographic detection and return oneRaycastHit[]
Structure.
Camera. The main. ScreenPointToRay and Camera. The main. ViewportPointToRay both ray detection with good understanding, is directly with the opening of the Camera, and then launch a ray, The parameter Vector3 pos is the direction in which the ray will be emitted. Commonly used is to click on the mouse position to launch a ray, more used for shooting and other games
Camera.main.ScreenPointToRay(Input.mousePosition); // Emit a ray from the camera, to the coordinate of the clickCopy the code
RaycastHit and bool Physics.Raycast in the project, it’s not enough to emit just one ray. When we emit a ray, we need to know what the ray is hitting, so we use Physics
The first three are commonly used. Let’s take a look at the parameters
Ray ray
: We create the ray aboveout RaycastHit hitInfo
Collision information returned by: (availabletransform
.rigidbody
.point
Etc.)float maxDistance
What is the maximum length of the rayint layerMask
: only the selectedLayermask
Colliders in layers are ignored by colliders in other layers. Selective collision
The int layerMask parameter is interesting, so let’s take a look. In Unity, every GameObject has a Layer. The Layer is one or several layers in the GameObject detection Layer. Any other Layer is not set to be detected, so it will not be detected. The rays will pass right through. LayerMask is actually very simple, roughly divided into four ways of expression:
- 1 << 10 Only detects layer 10.
- ~(1 << 10) detects layers other than the 10th.
- ~(1 << 0) detects all layers (optionally, all layers are detected by default).
- (1 < < 10) | (1 < < 8) detection and 8 of 10 layers.
Let’s look at an example: with a mouse click, we emit a ray from the camera. The ray only detects our custom Layer8: Cube and prints the name of the object detected by the ray
From the figure above, we can see that only one Cube object is detected, because only the Cube level is set to 8. If you are careful, you will notice that there is a red line in the Scene, which is drawn by debug. DrawLine
DrawLine debug. DrawLine is the method of drawing lines in the Scene view. The common method is as follows. This method is to draw a ray with the ray detection Debugshiyong, this function can only be used in the Scene view
public static void DrawLine(Vector3 start, Vector3 end, Color color, float duration);
Copy the code
- Start: the starting point of the line
- End: The end of the line
- Color: Line color
- Duration: The duration of the line
The source code
Source code for the Demo above
private RaycastHit objhit; private Ray _ray; private int layerMask = 1 << 8; void Update() { if (Input.GetMouseButtonDown(0)) { _ray = Camera.main.ScreenPointToRay(Input.mousePosition); If (Physics.Raycast(_ray, out objhit, 1000, layerMask)) {debug.drawline (_ray.origin, objhit.point, Color.red, 2); / / show a ray, can only see a GameObject in the scene view gameObj = objhit. Starts. The GameObject. Debug.Log("Hit objname:" + gameObj.name + ">>>>Hit objlayer:" + gameObj.layer); }}}Copy the code
Write in the last
All the shared content are the author in the daily development process used a variety of small function points, share out also in a disguised review, if there is a bad place to write also please give advice. Welcome to learn from each other and make progress.