Unity3D Summary: Ray collision

Source: Internet
Author: User

Method 1: Physics. Raycast Light Projection 1. static functionRaycast(Origin: Vector3,Direction: Vector3,Distance: Float = Mathf. Infinity,LayerMask: Int = kDefaultRaycastLayers): bool
A static radiation emission method cast a ray in the scene that can collide with all the colliding devices. Parameter description: origin: Ray start point direction: Ray direction distance: Ray length layerMask: select only the collision generator in the Layermask layer, and ignore the collision generator in other layers. Returns a Boolean value. It is true when a ray is projected at the intersection of any colliding devices. Otherwise, it is false. (Note: If a ray is projected from the inside of a ball body to the outside, false is returned. ) This test fails! 2. static functionRaycast(Origin: Vector3,Direction: Vector3,Out hitInfo: RaycastHit,Distance: Float = Mathf. Infinity,LayerMask: Int = kDefaultRaycastLayers): In the scenario, the bool drops a ray that can collide with all the colliding devices and returns the collision details.
Parameters

  • OriginThe starting point of the ray in world coordinates.
    In the world coordinates, the starting point of the ray.
  • DirectionThe direction of the ray.
    The direction of the ray.
  • DistanceThe length of the ray
    The length of the ray.
  • HitInfoIf true is returned, hitInfo will contain in more information about where the collider was hit (See Also: RaycastHit ).
    If true is returned, hitInfo contains more information about the server collision.
  • LayerMaskA Layer mask that is used to selectively ignore colliders when casting a ray.
    Only the dashboard in the Layermask layer is selected. The other dashboard is ignored.
Returns
It is true when a ray is projected to cross with any colliding devices. Otherwise, it is false.
Use the out keyword in c # To input an empty Collision Information class, and assign a value after the collision. You can obtain the transform of the collision object, Rigidbody, point, and other information.
For more information, see:LayerMask layer mask

Each object has a Tag and a Layer tag. A Tag is a simple tag that can be used to distinguish different objects. The role of LayerMask isFilters objects selectively, for example, when a ray is projected.For example, camera. cullingmask and ray projection.
You can edit tags and layers in TagManager.

Then, set the Layer level of the object, and set camera. cullingmask in the camera to control the rendering level of the camera. When used on the Ray, you can control what the ray collision is, not what the collision is.

// 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 ); }
}
In the above example, the coordinates of the objects bound to the current script are sent to the right, with a length of 100 rays. The ray collision level is 8, and other levels are ignored. In order to test, we draw a red line after the collision to the object, representing the ray, staying for one second. These are some basic operations of rays.

Method 2: Physics. RaycastAll ray projection static function RaycastAll( Ray: Ray, Distance: Float = Mathf. Infinity, layerMask: int = kDefaultRaycastLayers): RaycastHit []
Static function RaycastAll( Origin: Vector3, direction: Vector3, distance: float = Mathf. Infinity, layermask: int = kDefaultRaycastLayers): RaycastHit []

A ray is projected and all collisions are returned, that is, a ray is projected and a RaycastHit [] struct is returned.

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.