Unity Screen Ray collision

Source: Internet
Author: User

Layers Layer:

From the layers setting, it supports up to 32 layers. The values of the layer begin with 0^2,1^2, 2^2, and 3^3 in turn.

When the camera culling the Mask property

Nothing=–1

Evening = sum of all layers, the project starts with 7 layers set by default. The value is 0^2,1^2, 2^2, 3^3:6^2 = 279

If we choose the Defulat layer, the No. 0 position value 0^2 = 1

If we choose Defualt,transparent, the layer position 0,1 = 0^2+1^2 = 3

Combination:

Suppose this happens in the game: a,b,c we need to design a number to represent the [A,B,C,AB,AC,BC] 6 combination, we just need to put a=0,b=1,c=2. Based on the number of ^2 parties

A = 0, B = 1, C = 2,ab = 3,ac = 5,BC = 6

This allows us to know by a numeric value what permissions the role has.

A ray is emitted to the screen to detect collisions with the object:

Raycasthit get information about touching an object

Ray defines the rules for the Ray

Send a ray to the screen

BOOL Physics.raycast (Ray ray,out raycasthit hit, float distance, int layermask)

Code:

usingUnityengine;usingSystem.Collections; Public classTest:monobehaviour {Private intindex =0; //Use this for initialization    voidStart () {intindex = Layermask.nametolayer ("Uimode"); }        //Update is called once per frame    voidUpdate () {Raycasthit hit; Ray Ray .=Camera.main.ScreenPointToRay (input.mouseposition); //with the mouse as the origin, to the world to launch a 100f length of the ray, only the Uimode layer of the object to collide        if(Physics.raycast (Ray, outHit , 100f, index)) {Debug.Log ("objects colliding to the Uimode:"+hit.collider.gameObject.name); }    }}

Unity Screen Ray collision

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.