Unity3d Ray Detection

Source: Internet
Author: User

Here to record the relevant content of the radiographic examination:

X-ray detection The name is the ray to detect whether the collision with the intersection, and the collision with the collision device intersection, will return a true.

There are many uses of rays: for example, to detect jumps, and to jump to the ground by projecting a ray of radiation to the ground.

Shooting game can be determined by the length of the ray to determine whether the target object is hit, etc.

The main tool classes used are:

Physicsraycasthit Ray projection collisionRay Ray

The first of these is:

physics.linecast Linear projection

A ray projection from the start position to the end position, if interacting with the collision body, returns true.

  Debug.drawline (Transform.position, Line_floor.position, color.red, 1f);  bool  grounded = Physics.linecast (transform.position, Line_ Floor.position, 1  << Layermask.nametolayer (  " ground  )); 
        if (grounded)        {            Debug.logerror (" A collision occurred ");             }         Else {            debug.logerror (" end of collision ");        }

The second type:

A light that can collide with all the collider in the scene. can control the direction of projection and the length of projection

Vector3 fwd = transform. Transformdirection (-vector3.up);   bool grounded =  ten );
        if (grounded)        {            Debug.logerror (" A collision occurred ");             }         Else {            debug.logerror (" end of collision ");        }

The third type:

A light that can collide with all the collider in the scene and return details of the collision.

Raycasthit hit; BOOLGrounded = Physics.raycast (transform.position,-vector3.up, outHit ); //controllable projection distance bool grounded = Physics.raycast (Transform.position,-vector3.up, out hit,100.0);        if(grounded) {Debug.logerror ("there was a collision."); Debug.logerror ("Distance is:"+hit.distance); Debug.logerror ("the object being collided with is:"+hit.collider.gameObject.name); }        Else{debug.logerror ("End of collision"); }

Note that the information for the collider returned here is sequentially, returning the first collision first, and returning the second after the first collision is completed.

Physics.raycastall All light projections

Casts a ray and returns all collisions, that is, projecting the light and returning a raycasthit[] structure.

raycasthit[] hits; Hits= Physics.raycastall (Transform.position,-vector3.up,100.0F); inti =0;  while(I <hits. Length) {Debug.logerror ("there was a collision."); Raycasthit Hit=Hits[i]; Debug.logerror ("the object being collided with is:"+hit.collider.gameObject.name); I++; }

Unity3d Ray Detection

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.