X-ray collisions: Unity3d Summary

Source: Internet
Author: User
Tags bool

Method One: Physics.raycast ray projection

1.static function Raycast (Origin:vector3, Direction:vector3, distance:float = mathf.infinity, Layermask:int = kDe Faultraycastlayers): BOOL

A static beam-emitting method that casts a light on a scene that can collide with all collider.

Parameter explanation:

Origin: Ray starting point

Direction: Ray Direction

Distance: Ray length

Layermask: Only the collider within the Layermask layer is selected, and other layers of the collider are ignored.

Returns a Boolean type that is true when a light project crosses any collider, otherwise it is false.

(Note: If the light is projected from the interior of a sphere to the outside, the return is false.) This test is not successful!

2.static function Raycast (Origin:vector3, Direction:vector3, out hitinfo:raycasthit, distance:float = Mathf.infin ity, Layermask:int = kdefaultraycastlayers): bool

The scene casts a light that can collide with all the collider and returns details of the collision.

Parameters parameters

Origin

The starting point of the "Ray in" world coordinates.

In world coordinates, the starting point of the ray.

Direction

The direction of the ray.

direction of the Rays.

Distance

The length of the ray

The length of the rays.

Hitinfo

If true is returned, Hitinfo'll contain more information about where the collider was hit (??).

If returned True,hitinfo will contain more information about the collision of the bump.

Layermask

A Layer mask it used to selectively ignore colliders when casting a ray.

Only the collider in the Layermask layer is selected, and other layers of the collider are ignored.

Returns

True when a light project crosses any collider, otherwise it is false.

Back to the column page: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/extra/

Use the Out keyword in C # to pass in an empty collision information class, and then assign a value after the collision. Can obtain the transform,rigidbody,point such as the collision object information.

A summary of-unity3d learning in X-ray collision

Explain in detail: Layermask layer Mask

As the picture shows, each object has a tag and layer tag, tag is a simple label, can be used to distinguish between different objects, the role of Layermask is to selectively filter objects, such as when projecting rays, such as camera.cullingmask and light projections.

As shown in the figure, you can edit tag and layer in Tagmanager

Then set the layer level of the object, set the Camera.cullingmask in the camera, you can control the rendering level of the camera, used in the ray, you can control what the Rays collide, do not collide.

Raycast hit

Raycasthit hit;

Layermask mask = 1 << 8;

void Testray () {

if (Physics.raycast (transform.position,vector3.right,out

hit,100, Mask.value)) {

Debug.drawline (transform.position,hit.point,color.red,1);

}

}

The example above is to make punctuation from the object that the current script binds to transmit a ray of 100 to the right, the Ray collision level is 8, and other levels are ignored.

To test, we draw a red line after colliding with the object, indicating the rays and staying for a second.

These are some of the basic operations of the Rays.

Method Two: Physics.raycastall ray projection

static function Raycastall (Ray:ray, distance:float = mathf.infinity, Layermask:int = kdefaultraycastlayers): Rayca Sthit[]

static function Raycastall (Origin:vector3, Direction:vector3, distance:float = mathf.infinity, Layermask:int = KD efaultraycastlayers): raycasthit[]

Casts a ray and returns all collisions, that is, projecting light and returning a raycasthit[] structure.

Author: csdn Blog Unity3d Learner

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.