Unity3d code for dragging an object with the mouse

Source: Internet
Author: User
This is a drag object. Code , Which is concise and clear. It helps you understand the unity3d script concept! Add comments here to share! VaR mouseovercolor = color. blue; // declare the variable to Blue private var originalcolor: color; // declare the variable to store the original color function start () {originalcolor = Renderer. sharedmaterial. color; // get the object color at the beginning} function onmouseenter () {Renderer. material. color = mouseovercolor; // when the mouse slides, the color of the object is changed to blue.} function onmouseexit () {Renderer. material. color = originalcolor; // restores the original color of the object when the mouse slides out.} function onmousedown () {var screenspace = camera. main. worldtoscreenpoint (transform. position); // convert 3D object coordinates to screen coordinates // convert the mouse screen coordinates to three-dimensional coordinates, and then calculate the distance between the object position and the mouse var offset = transform. position-camera. main. screentoworldpoint (vector3 (input. mouseposition. x, input. mouseposition. y, screenspace. z); print ("down"); While (input. getmousebutton (0) {// print ("button0"); var curscreenspace = vector3 (input. mouseposition. x, input. mouseposition. y, screenspace. z); var curposition = camera. main. screentoworldpoint (curscreenspace) + offset; transform. position = curposition; yield; // This is very important, loop execution }}

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.