Public classshexian:monobehaviour {Camera cam;//a camera for emitting rays voidStart () {Cam= This. Getcomponent<camera>(); } voidUpdate () {if(Input.getmousebuttondown (0) ) {tarraycast (); }} Vector3 MP;//Mouse PositionTransform Targettransform;//the selected object . PublicLayermask Targetinglayermask;//Specifies the layermask layer that the ray can detect Private floatTargetingraylength = mathf.infinity;//the length of the Ray Public voidTarraycast () {Targetinglayermask.value=1<<Ten;//do not understand the bottom of the few chestnut ~MP =input.mouseposition; Targettransform=NULL; if(Cam! =NULL) {Raycasthit hitinfo; Ray Ray .= Cam. Screenpointtoray (NewVector3 (mp.x, MP.Y, 0f)); if(Physics.raycast (Ray.origin, Ray.direction, outHitinfo, Targetingraylength, Targetinglayermask.value)) {Targettransform=HitInfo.collider.transform; } } //when you get the selected object, manipulate it . if(Targettransform! =NULL) {Debug.Log (targetinglayermask.value); Debug.Log (Targettransform.name); Debug.Log ("always feel that this is actually Tianjin Rice Cave of the hole ~"); } }}
About the use of layermask, simply give a chestnut ~
:
1 << 10 Open 10th floor.
~ (1 << 10) opens a layer other than the 10th.
~ (1 << 0) Open all layers.
(1 << 10) | (1 << 8) Open the 10th and 8th layers.
< ( ̄3 ̄) > Well, what about the chestnuts!
"Unity" Mouse click to select objects