Summary of Ray collision detection in Unity

Source: Internet
Author: User

Now by watching video, reading to unity in the Ray collision detection, there is some understanding, here I summarize it to write down, hope to help you, but also hope that through the great God to correct the shortcomings;

Ray collision detection, that is, by a certain object emitted a ray, the ray collision to the object, you can get information about the object, and then you can do some operation of the object process. PS: Personal Insights;

The emission of radiation is divided into visible and invisible, the visible situation is in the game you can see the ray, and not visible is the game running, you can only try to observe it in the scene, and this to the ray is there;

In fact, the difference is not big, the key is to see how you use it;

Let's take a look at the invisible situation, or look at the code;

ray ray=new ray (transform.position,transform.forward*100);           //defines a Ray object, Contains the position of the ray emission transform.position, the transmitting distance transform.forward*100;debug.drawline (transform.position,transform.position+ transform.forward*100,color.red);   //This is the ray that is plotted, including the launch position, the emission distance and the color of the ray;raycasthit hitinfo;                                   // Define a Raycasthit variable to hold the information of the object being bumped; if (Physics.raycast (ray,out hitinfo,100))           //If a collision is made to an object, the Hitinfo contains information about the object; {    //hitinfo.point: The location of the collision point;     //hitinfo.normal: A vector perpendicular to the plane of the collision point;     //hitinfo.collider.gameobject: Can get all the information on the object;} 

  another case: the need to add a Linerenderer component on the object that emits the ray;

Linerenderer render;void start () {    render=transform. Getcomponent<linerenderer> ();     render. SetWidth (0.01f,0.01f);      //setwidth (startwidth,endwidth);     Render. SetColor (color.red,color.red);    //setcolor (Startcolor,endcolor);     render . Setvertexcount (2);               //set vertex number} Void update () {        ray ray=new ray ( TRANSFORM.POSITION,TRANSFORM.FORWARD*100);           // Defines a Ray object that contains the position of the ray emission transform.position,                                                                               //  Launch Distance transform.forward*100;                                                                          RaycastHit  hitinfo;                                  // Define a Raycasthit variable to hold the information of the object being bumped;         if (Physics.raycast (ray,out  hitinfo,100))          //if the object is colliding, the Hitinfo contains information about the object;         {             //hitinfo.point: Location of the collision point;             //hitinfo.normal: Vector;    perpendicular to the plane of the collision point          //hitinfo.collider.gameobject: All the information on the object can be obtained;             if (Input.getmousebuttondown (0))              {                 render. SetPosition (0,transform.position);             // The starting position of the ray;                 render. SetPosition (1,transform.position+transform.forward);     //Ray Direction;            }             render. Setvertexcount (0);                  //eliminate Rays;         }    }

OK, simple, the code is I write directly on this, there is anything wrong, please correct me Oh! Joint efforts, yes 650) this.width=650; "src=" Http://img.baidu.com/hi/jx2/j_0059.gif "alt=" J_0059.gif "/>

This article is from the "51cto_king" blog, make sure to keep this source http://cjboking.blog.51cto.com/11020113/1784387

Summary of Ray collision detection in Unity

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.