Unity physics.raycast Ray projection

Source: Internet
Author: User



BOOL Physics.raycast (Vector3 origin, Vector3 direction, float distance, int layermask)


True if Ray casts intersect with any collider, otherwise false.

BOOL Physics.raycast (Ray Ray, Vector3 direction, raycasthit out hit, float


distance, int layermask)


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


BOOL Physics.raycast (Ray Ray, float distance, int layermask)


True if Ray casts intersect with any collider, otherwise false.


BOOL Physics.raycast (Vector3 origin, Vector3 direction, raycasthit out Hit,float


distance, int layermask)


True if Ray casts intersect with any collider, otherwise false.




Note: If the light is cast from the inside of a spherical body to the outside, it is returned as false.


Parameter understanding:


Origin: The starting point of the ray in world coordinates


Direction: direction of the Ray


Distance: Length of the Ray


Hit: Use the Out keyword in C # to pass in an empty collision information class, then assign a value after the collision. Can get a collision object


Transform,rigidbody,point and other information.


Layermask: Only the collider within the Layermask layer is selected, and the other in-layer collider is ignored. Selective collisions



Raycasthit[] Raycastall (Ray Ray, float distance, int layermask)


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


The following is a small example of a pickup made using a ray (drag the code directly onto the main camera)

Using unityengine;using system.collections;public class raytest : monobehaviour  {    // use this for initialization    void  Start  ()  {        }         // update is called once per frame    void  Update  (     {        if) ( Input.getmousebutton (0))         {             ray ray = camera.main.screenpointtoray (Input.mousePosition) ;//X-ray from camera to click coordinates             RaycastHit  Hitinfo;            if (Physics.Raycast (ray,out  hitinfo))    &Nbsp;        {                 debug.drawline (ray.origin,hitinfo.point);//underline the ray, only in scene view can see                 gameobject  gameObj = hitInfo.collider.gameObject;                 debug.log ("click object name is "  +  Gameobj.name);                 if (gameobj.tag ==  "boot")//When the Ray collision target is the boot type of the item  , perform a pickup operation                  {                     debug.log ("pick up!");                 }             }        }    }}


Unity physics.raycast Ray projection

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.