Principle of Unity3d ray, basic usage

Source: Internet
Author: User

Ray: A ray is a zero-end line emitted by a point in the 3D world in One direction, which stops firing when colliding with other objects in the launch trajectory.

Ray Rays and Raycasthit are two of the most common tools used in the shooting.

Uses: Ray is used for collision detection (such as whether to hit the target in the shooting game), the role movement, to determine whether to touch the object in the 3D world, and then operation.

Basic API:

Ray Ray class: "Using a mouse to pick up or determine whether the projectile hit the image, you need to shoot in a specific direction." The direction may be a vector direction in the world's coordinates, or a point on the screen. The pair provides two APIs, as follows: "

1.Ray Camera.main.ScreenPointToRay (Vector3 POS) Returns a ray ray from the camera's near mouth nearclip to the screen to specify a point; if the line does not collide with the object, The value of the collision point Hit.point is (0,0,0); Pos: Uses the actual pixel value to represent the location of the shot to the screen. The X or Y component of the POS is increased from 0 to the maximum, and the route is moved from one side to the other. But POS is on the screen, so the z component is finally 0;

2,Ray Camera.main.ViewportPointToRay (Vector3 POS) Returns a ray from the camera to the viewport (invalid outside the viewport) to specify a point; POS: The position of the route to the screen is expressed in the form of a bitwise proportional value.

Raycasthit Ray projection collision information:

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

The route returns true when colliding with the collider, otherwise it returns false if no collider is worn. Origin: The beginning of the ray in world coordinates; direction: The direction vector of the ray;Distance: The length of the ray, that is, the distance from the point to the end, which is infinite;layermask: The layer mask, the collision of the specified layer, Other layers of collisions will be ignored.

2. 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 information about the collision, including the location.

3. bool Physics.raycast (Ray Ray, float distance, int layermask)

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

4, 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.

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. Information such as Transform,rigidbody,point of colliding objects can be obtained.

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

5,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.

6,Layermask: A total of 32 layers, the use of a 32-bit certificate of each of the bits to represent each level, likeness this bit for 1 means using the layer, 0 means not to use the layer.

Drag the code directly onto the main camera, or other images:

usingUnityengine;usingSystem.Collections; Public classRaytest:monobehaviour {voidStart () {}voidUpdate () {if(Input.getmousebutton (0) {ray Ray= Camera.main.ScreenPointToRay (input.mouseposition);//The ray from the camera to the click coordinatesRaycasthit Hitinfo; if(Physics.raycast (Ray, outhitinfo)) {debug.drawline (ray.origin,hitinfo.point);//scene view to seeDrawLine (Vector3 Origin,vector3 end,color col): When Gion has a likeness collision, you can see the shot in the scene view.
                Gameobject OBJ = hitInfo.collider.gameObject; Debug.Log ("" +if"xx")// when the Ray collision target is the image of the name XX, perform the following actions {dosomething ...}} } } }

If not, please help me.

The solution of the route is very clear a link: 53317996

Principle of Unity3d ray, basic usage

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.