- Home /
Move objects with mouse while paused (2D)
How would I be able to make specific objects in the game world (marked with its own material) move using the mouse while in game?
I'm not sure what part of this problem you are asking for help with. I assume that you paused the game by setting Time.timeScale = 0.0? If so you will still get Update() calls and can use any drag and drop code to move your object that does not depend on Time.deltaTime (which is most drag and drop code). There are a number of posts on UA with drag and drop code.
I need to know how to do the two together. I've got pausing worked out so far.
Answer by robertbu · Jan 15, 2014 at 01:46 AM
Any drag and drop code you find (and it has been posted to UA many times), that use OnMouseDown() rather than raycasting will work in 2D. Raycasting solutions would need a bit of change for 2D. Here is one link. The script will need to be attached to each object you want to be draggable:
http://answers.unity3d.com/questions/486097/mouse-clicking-on-the-plane-and-drag-to-other-posi.html
Note if you only want to only have it draggable if the game is paused, then you can add something like this to the top of the OnMouseDown() callback:
if (Time.timeScale > 0.01) return;
I can't seem to find any answers which use On$$anonymous$$ouseDown rather than Raycasting. Also, with the one you linked, It is only working on the X axis and is ignoring collision detection.
The code at the link will work for most common and some uncommon situations. The only situation I can think of that it will not work for is the camera looking down on the XZ plane. Note I tested it with a standard 2D setup with a Sprite and with the camera facing positive Z and it worked fine for tracking the mouse on both 'X' and 'Y'.
Collision detection is another story. Nothing in your original question suggested that collision detection was a requirement. In order to make collision detection work you may have to go to a 2D rewrite of the standard DragRigidbody.js script. Available:
Assets > Import Package > Scripts
The 2D stuff is new, so I'm still learning about how certain problems are solved.