"Unity" Mouse click to select objects

Source: Internet
Author: User

 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

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.