Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Game panel realizes mouse dragging objects

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

It is well known that dragging objects can be achieved in the Unity scene panel (Scene panel). The specific operation step is to transform the toolbar to select the "hand" drag tool (or the shortcut key "Q"), and then directly use the left mouse button to press and hold down the drag object. Of course, this effect does not actually realize dragging the object, because the position of the object does not change. It actually moves our current scene and looks like it's dragging an object. As shown in the figure:

So now we want to implement how to implement mouse drag objects in the Game panel? Actually, the code is very simple. Directly above:

As shown in the picture, we hang the script on the object we want to drag (it must be on the object to be dragged). Then click to run, put the mouse pointer over the object, press and hold the left button to achieve. As shown in the figure:

The main method is to use the cooperative function. OnMouseDown () is a mouse-triggered callback function. It can be realized without calling this method. So there is no writing to turn on synergy.

The method can be triggered once when the mouse pointer is pressed over the object. Then we need to ask for the distance between the mouse position and the object being dragged, so as long as the distance between the two is kept the same. And because the mouse is in the screen coordinate system, the object is in the world coordinate system. So there needs to be a conversion between the two. All are converted to the world coordinate system and then the distance between the two is calculated (the details of the picture notes have been pointed out).

The while loop enters when the mouse is pressed, because the Bool value is always true, but because of coordination, every frame has to jump out of the function, so there is no endless loop. Finally, wait for the FixedUpdate () function. It is also possible to write yield return null directly. The goal is just to jump out of the synergy and then enter the next frame.

This is the simple implementation of mouse drag objects.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report