Unity Ray Detection

Source: Internet
Author: User

A tool that is very practical and often useful for ray detection in unity. Here's a look at the ray detection problem.

1) Ray

Defines a ray based on the direction of the ray's endpoints and rays

New Ray (Transform.position, Transform.forward);

Define a variable Raycasthit hit with ray projection information and perform a radiographic inspection physics.spherecast

Raycasthit hit;
if (Physics.spherecast (Ray,1f,out hit))
{
if (hit.distance<3f)
{
float angle = Random.range (90, 110);
print (angle);
Transform. Rotate (0, angle, 0);
}
}

A hit can be obtained by using the structure variable hits to obtain the relevant data after the ray touches the object. Point (collision dot coordinate), Hit.transform.gameObject (Collision object, which in turn can get the script component on the game object, which can call the method or variable on the component);

Ray detection can be used raycast can also be used spherecast, specific what type of ray projection can be selected according to the needs of their own

1) Screen Ray

The screen Ray is a ray with a camera.

            Ray Ray = Maincamera.screenpointtoray (point);            Raycasthit hit;             if  out Hit ))            {                Startcoroutine (createonshow (Hit.point));                 = Hit.transform.gameObject;                 = Hitgobj.getcomponent<reacttohit>();                 if (newreactor!=null)                {                    newreactor.react ();                }            }

Unity 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.