“This is the 18th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021”

The Unity Physics System — Ray Introduction

Some structures (classes) related to Ray:

  • Physics: RayCast () emits a ray;
  • Ray (Struct) : Object representing the Ray itself
  • RaycastHit (struct) : Collision information for rays and other colliders

Plotting ray function

  • Debug.DrawLine(transform.position,Vector3.forward * 10,Color.blue,10f);
  • Debug.DrawRay(transform.position,Vector3.left * 10,Color.red);

Note:

  1. The first parameter is the drawn position
  2. The second argument is the drawn point, which defaults to 1 and can be multiplied by a value of type float to increase its ray length
  3. The third parameter is color
  4. The fourth parameter is the ray region. If you change the direction of the ray, the ray will form a region of type float. The larger the value, the longer it will stay.

Ray detection

Related parameter concepts of radiographic detection:

  • RaycastHit hit: Raycast collision (this variable is used to store information about which objects this ray hits)
  • Camera. The main. ScreenPointToRay (position) : screen location shooting line: return a ray from the Camera through a screen

Note:

  1. The resulting rays are in world space, starting from the near clipping plane of the camera and passing through the screen position(x,y) pixel coordinates (Position.z is ignored).
  2. Screen space is defined in pixels. The bottom left of the screen is (0,0); The top right is (pixelWidth,pixelHeight).

The API:

  1. Ray Camera. Main. ScreenPointToRay Vector3 (pos) returns a Ray Ray from the Camera to the screen to specify a point
  2. Ray Ray class
  3. RaycastHit ray projection collision information
  4. Bool Physics.Raycast Note: If a ray is cast from the inside of a sphere to the outside, return false.

Parameter Understanding:

  • Origin: The starting point of a ray in world coordinates
  • Direction: indicates the direction of the ray
  • Distance: ray length
  • Hit: an empty collision information class is passed in using the out keyword in c# and then assigned after the collision. Can get collision object transform, rigidbody, point information, etc.
  • LayerMask: Only colliders in layerMask layer are selected. Colliders in other layers are ignored. Selective collision filters objects. You can edit tag and Layer in the TagManager. Then set the Layer level of the object, set camera. Cullingmask in the camera, can control the rendering level of the camera, used in the ray, can control what ray collision, what not collision.
  • Returns a Boolean type, true when the raycast crosses any collider, false otherwise. Note: if a ray is cast from the inside of a sphere to the outside, return false.

Ray’s 5 parameters:

RaycastHit[] RaycastAll(Ray ray, float distance, int layerMask)

Cast a ray and return all collisions, that is, cast the ray and return a RaycastHit[] structure.

  • Physics.Raycast(distance); Physics.
  • Physics.Raycast(position, direction forward, out hit, ray length distance);
  • Physics.Raycast(position, direction forward, out hit, ray length distance, layerMask 1<<8);

Note:

  1. The first parameter is the starting point of the ray
  2. The second parameter is the direction of the ray
  3. The third parameter is the collision information
  4. The fourth parameter is the length of the ray
  5. The fifth argument is the mask filter layer (written in binary form, so we need to use a simple left shift operation (<<))
public class RayDemo : MonoBehaviour 
{

    // Create a plane, and when you click on a point on the plane, fire a bullet from the camera at that point.
    Ray ray;
    RaycastHit rh;

	void Start ()
        {
            //ray = new Ray(Vector3.zero,Vector3.forward); // Create a ray,

            //Physics.Raycast(Vector3.zero, Vector3.forward);
            / / if (Physics. Raycast (0.5 f) ray out rh,)
            / / {
            // print(rh.point); / / collision point
            / /}
	}

    void Update()
    {
        ray = Camera.main.ScreenPointToRay(Input.mousePosition);   // Mouse position emission line

        if (Input.GetMouseButtonDown(0))                     // Press the left mouse button
        {
            if (Physics.Raycast(ray, out rh, 10f))           // Emit a ray
            {
                Destroy(rh.transform.gameObject);            // The ray disappears when it hits an object
                Debug.Log(rh.point);                         // Prints the collision point}}}}Copy the code

Set the ray by layer:

public class Ray1Demo : MonoBehaviour
{
    GameObject go;
    
    void Start()
    {
        go = GameObject.Find("Cube2");
    }
    
    void Update()
    {
        go.transform.Rotate(new Vector3(0.1.0));
        RaycastHit hit;
        Vector3 fwd = transform.TransformDirection(Vector3.forward);
        
        if (Physics.Raycast(transform.position, fwd, out hit, 100.1 << 8))
        {	
            //1 displacement 8, this parameter indicates that only the layer in the parameter can be detected
            // Ray collision level 8, other levels ignored.Destroy(hit.collider.gameObject); }}}/* Note: 1 << 10 opens layer 10. ~(1 << 10) opens the layer except for layer 10. The default layer, can want to manually add (1 < < 10) | (1 < < 8) open and 8 of 10 layers. // Ray collision level 8, other levels ignored. Destroy(hit.collider.gameObject); Note: 1 << 10 opens layer 10. ~(1 << 10) opens the layer except for layer 10. The default layer, can want to manually add (1 < < 10) | (1 < < 8) open and 8 of 10 layers. * /
Copy the code