Unity3D: Physical rays and unity3d rays

Source: Internet
Author: User

Unity3D: Physical rays and unity3d rays

RAY:From a point to a direction, a ray of infinite length is emitted, which will collide with an object with a Collider component in the scene.

Use of rays:

1 public class Test 2 {3 private void Start () 4 {5 if (Input. getMouseButtonDown (0) 6 {7 // emits a Ray from the main camera to the position clicked by the mouse. 8 Ray ray1 = Camera. main. ScreenPointToRay (Input. mousePosition); 9 // emits a Ray in the forward direction of the world axis from its position. 10 Ray ray2 = new Ray (this. transform. position, Vector3.forward); 11 // declare an RaycastHit variable to store the collision information of rays. 12 RaycastHit hitInfo; 13 // check whether the rays collide with the object 14 if (Physics. raycast (ray1, out hitInfo) 15 {16 // execute code 17} 18} 19} 20}

According to the above Code:

HitInfo. point:Coordinates of collision points.

Physics. Raycast ():When this method is used to detect rays, because the method has a lot of heavy loads, you must find out which heavy load method you are using and fill in the correct parameters. Otherwise, errors may occur easily.

 

Because the rays are invisible, we can simulate the rays by drawing a line to facilitate debugging. The method is as follows:

Debug. DrawRay (Vector3 pos, Vector3 dir, Color c ):Draw a line.

Pos: Start coordinate.

Dir: direction.

C: color.

Related Article

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.