"Unity" mouse pointing to an object, displaying the name of the object, etc.

Source: Internet
Author: User

has been used Ngui HUD text plugin to do this function, feel a small function on a plug-in is simply insane. Then I wrote a ~ idea and plug-in the same way. But Ah but ah ~ My this small idea seems definitely more convenient than the plug-in AH ~ because Ah, I have this one function Ah ~

Waiter, on the code ~ ~ ~

Camera Cam;//a camera for emitting raysCamera Uicam;//the camera at the UI layerVector3 MP;//Mouse PositionTransform Targettransform;//the selected object .     PublicUILabel Lab; voidStart () {Cam= This. Getcomponent<camera>(); Uicam= Gameobject.find ("Camera"). Getcomponent<camera>(); Lab= Gameobject.find ("Label"). Getcomponent<uilabel>(); }    voidUpdate () {if(Tarraycast ())//determine if the mouse refers to an object        {            //when the coordinates are converted, I use the "specify the object y-axis upward 0.3f", of course, only the general guarantee of the matching position, the best way is to manually specify, that is, manually drag an empty object to the "object that needs to be clicked", fixed its suitable position, and then coordinates with the empty object of ~Vector3 pos = Cam. Worldtoviewportpoint (Targettransform.position +NewVector3 (0, Targettransform.localscale.y/2+0.3f,0)); Lab.transform.position=Uicam.viewporttoworldpoint (POS); Lab.text=Targettransform.name; }    }    //Ray detection part, do not understand can see my previous article ~     Public BOOLTarraycast () {Lab.text=NULL; 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)) {Targettransform=HitInfo.collider.transform; return true; }        }        return false; }}

"Unity" mouse pointing to an object, displaying the name of the object, etc.

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.