Unity3d Ray assigned layer get Gameobject note

Source: Internet
Author: User


This is a purely technical discussion, and it should be clear that the children's shoes that have read my previous article my run is by the mouse response, the mouse point to where to run, and then have the interface, the trouble came, my interface unexpectedly can Dianchuan, I do not want to point to the interface when the surface can point to the interface behind the ground, the role of the silly pull a few to run, that swollen? , you can not always click to judge the name of the object click, that interface more will be exhausted, fortunately we have the label and layer of the two artifacts, so I introduce two options, can achieve the effect.


Scenario One: Use tags to intercept ray messages.

The Inspector panel at the top of each gameobject has a tag option, which means that we can add a label to all the objects, Unity3d has a few tabs by default, where we can customize the UI tag and then use that tag for all the UI. Click on the option to the right of Tag and choose Add tag,size to change the value of 2,element 0 to UI.


Then all of the UI you don't want to be Dianchuan is chosen as this tag.

In addition to setting the label, it is also important that the interface to add Collider Collider, some UI default has a collider, such as the Ngui button, you can see there is a box Collider, if not you have to add, size there to adjust the size of this UI:


So the UI can receive a ray collision, and then the code can write:

[CSharp]View Plaincopy
  1. Ray ray1 = nguiCamera.camera.ScreenPointToRay (input.mouseposition);
  2. Raycasthit hit1;
  3. if (Physics.raycast (ray1, out hit1)) {  
  4. if ( hit1.collider.gameObject.tag = = "UI")
  5. return;  

The above nguicamera is the camera under the Ngui, because it is the camera's rays that collide with the UI, not the main camera, which is important to note. The others do not explain, very simple, see for themselves.


Scenario two, using layers (layer) to intercept ray messages

Each Gameobject Inspector Panel has a layer option at the top, just next to tag, Unity3d already has a few layers, we create a new layer, also called UI, click on the add layer, you can see from Layer0 to Layer7 are gray out, That is not available, from the eighth can be used, so in the eighth to build a UI layer.


Then we look at the parameters of the physics Raycast:

static function Raycast (Ray : Ray, Out Hitinfo : Raycasthit, Distance: float = Mathf.infinity, Layermask: int = kdefaultraycastlayers): bool

In general, we only use the first two parameters, distance represents the ray distance, the default is infinity, the focus is the last parameter layermask, specifically dealing with layer filter, is an integral type, how to use it, is by the layer of the bits to operate, see the following code is clear:

[CSharp]View Plaincopy
  1. Ray ray1 = nguiCamera.camera.ScreenPointToRay (input.mouseposition);
  2. Raycasthit hit1;
  3. Layermask mask = 1 << layermask.nametolayer ("UI"  );  
  4. if (Physics.raycast (ray1, out hit1, Mask.value)) {
  5. return;  

The Nametolayer of Layermask is to return the index of the layer by the name of the layer, here is 8, then 1<<8 converted to Layermask value, then the value of Layermask can be.

Note that you must also set collider to receive collisions before you can tell.

I am here to provide two programs, if there are other options welcome message, Thank you.

The author supplements the content
If you are creating a method in your code that gameobject specify a layer and get a layer to take a ray of attention place.
Create a gameobject as follows:

Gameobject _newobj = new Gameobject ();
_newobj.layer = Layermask.nametolayer ("Commobj");//Specify Layer
_newobj.name = "Terrain0_grass";

Print (Layermask.nametolayer ("Commobj"));

Boxcollider _collider = _newobj.getcomponent ();
_collider = _collider = = null? _newobj.addcomponent (): _collider;
_collider.center = Vector3.zero;
_collider.size = new Vector3 (10f, Commondef.layer0_terrain_grass, 10f);

Meshfilter _meshfilter = _newobj.getcomponent ();
_meshfilter = _meshfilter = = null? _newobj.addcomponent (): _meshfilter;

Mesh _mesh = Modelresmanager.addterraingrassmesh (10, 10);
_meshfilter.mesh = _mesh;

Meshrenderer _meshrender = _newobj.getcomponent ();
_meshrender = _meshrender = = null? _newobj.addcomponent (): _meshrender;

To point to a parent-child node relationship, simply specify its location component parent-child relationship;
Gameobject _subobj = gameobject.createprimitive (Primitivetype.cube);
_subobj.transform.parent = _newobj.transform;

The above is to create a gameobject, assign the collider component for Ray collision detection, here is the method to get the specified layer ray:

Layermask mask = 1 << layermask.nametolayer ("Commobj");

Ray Ray = Camera.main.ScreenPointToRay (input.mouseposition);
Raycasthit _hitinfo;
if (Physics.raycast (ray, out _hitinfo, 1000f, Mask.value))
{
M_currselectobj = _hitinfo.collider.gameobject;
M_mousemovepos = _hitinfo.point;


Bold to get the mask value of the specified layer for Ray detection.

Unity3d Ray assigned layer get Gameobject note

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.