"Unity 3D" Learning note 40: Ray

Source: Internet
Author: User

Ray

Ray, the analogy of understanding is the bullets in the game. is a line in the 3D world in which a point is fired in one direction without an end. In the course of the launch, once it collides with other objects, it stops firing.


The principle of Ray

When creating a ray, you first need to know the coordinates of the beginning and end of the ray in the 3D world.

Using unityengine;using System.collections;public class Script_06_08:monobehaviour {void Update () {//create ray, emitted from 0 to object Ray Ray = new Ray (Vector3.zero, transform.position);//Calculate the start and end point of the Ray Raycasthit hit; Physics.raycast (Ray, out hit, 100);//Use the Debug method to draw this line (the debug method records the presence in the scene view) Debug.drawline (Ray.origin, Hit.point);}}
After execution:


In the above code, the Debug.drawline () method is only visible in the scene view. It is a false idea to draw the Rays in the game, using the GL graphics library or the Linerenderer method.



Collision Monitoring

A ray can be used to infer collisions with other objects in the game, such as the camera's position as the origin, and a ray emitted to the mouse moving point. It's like hitting a bull's-eye and inferring if it's hit.

Using unityengine;using System.collections;public class Script_06_09:monobehaviour {//Bullseye map Public Texture texture;// Hint Information private string info;void Update () {//create Ray Ray Ray = Camera.main.ScreenPointToRay from camera position to mouse position  ( Input.mouseposition); Raycasthit hit;//Infers if the ray is hitting the game object if (Physics.raycast (Ray, out)) {info = "Hitting the bull's Eye";} Else{info = "Missed Bull's Eye";}} void Ongui () {//calculate the coordinates of a quasi-heart map rect rect = new Rect (input.mouseposition.x-(Texture.width >>1), Screen.height-input.mou SEPOSITION.Y-(texture.height >> 1), texture.width,texture.height);//Draw quasi-heart map gui.drawtexture (rect,texture);// Enter the target information Guilayout.label (info + ", hit the coordinates:" +input.mouseposition);}}
After execution:

The code above uses the Camera.main.ScreenPointToRay method to wear a ray emitted by the camera to the current position of the mouse, and then uses Physics.raycast to infer whether the ray intersects a game, the intersection returns TRUE, and No returns FALSE.


"Unity 3D" Learning note 40: Ray

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.