Unity a fourteen-ray

Unity provides the radiographic features.
new Ray(transform.position,transform.forward);A new ray, determining the initial position of the beam, and the direction of the radiation.

Physics.Raycast, emitting radiation, the radiation length can be provided, important ray method, there are many types this parameter.
Here Insert Picture Description
First with three parameters.
The first parameter represents: New rays, the second parameter represents: an impactor ray (Note that parameter to take out). The third parameter represents the distance rays.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RayTest : MonoBehaviour
{
    // Start is called before the first frame update
    private RaycastHit raytest;
    void Start()
    {
          Ray a = new Ray(transform.position,transform.forward);
    if(Physics.Raycast(a,out raytest,200)){
            Debug.Log(raytest.point);
           Debug.Log(raytest.transform.name);
        }
    }
    

}


Then a fourth argument.
The first three parameters above, the fourth argument: on behalf of the screening level, which is the ray collides with an object will have a reaction.
Here Insert Picture Description
We let the figure above is irradiated to the ball body of the capsule, we first ray is shot in the hierarchy of objects (i.e. the capsule body level) is set to Test, the following hierarchy can be acquired in two ways.
1, int b = LayerMask.GetMask("test","test1");
2, int c = (1 << 9) | (1 << 10);i.e. 2 9 th |10 th power of 2, the resulting value is equal to: 9 +2 th power of 2 10 = 1536

b and c values ​​are equal.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RayTest : MonoBehaviour
{
    // Start is called before the first frame update
    private RaycastHit raytest;
    void Start()
    {
        Ray a = new Ray(transform.position,transform.forward);
        int b = LayerMask.GetMask("test","test1");
        int c = (1 << 9) | (1 << 10);
        
        Debug.Log("b::::::::"+b+",,,,,c::::::"+c);
     
       if(Physics.Raycast(a,out raytest,200,b)){
     
           Debug.Log(raytest.transform.name);
        }
    }
    

}

Published 56 original articles · won praise 24 · views 30000 +

Guess you like

Origin blog.csdn.net/u014196765/article/details/93212270