Unity Tutorial : Drag Gameobject with Mouse

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys this is a new unity tutorial in which I want to show you how to drag game objects in your game around with the mouse here's my demo scene with the stone and the rock both have colitis attached and what I will do now is to add a new c-sharp script to the project that are attached to the game object so that I'm able to drag them around what the game is running I call the script drag object and then I open it in Visual Studio okay here's the class called drag object we don't need the start and update methods but add another one which is called on Mouse drag and as the name implies this is called when the object is dragged with the mouse in this method I want to set the position of the object to the position of the cursor and add a certain offset but what is this offset it is basically the difference between the position of the game object and the cursor in the 3d world this is a vector of 3d and we can store this in a member variable and the left mouse button is pressed to be notified about this event you can implement the method on Mouse down now we have to keep in mind that we are working with coordinates in the 3d world but the position of the cursor is a screen position x and y so what I do to get the mouse position in the 3d world is to implement a method that I call get Mouse world pause in this method I get the mouse point from the input clause but as I said we have to convert this from pixel coordinates to world coordinates and for the set coordinate we want to use the one from our game object this can also be stored when the left mouse button is pressed so create another member variable of type float for this and assign it here in the onmousedown method to get it as a screen point i used the world to screen point method of the main camera and in the method get' mouse world pause i use it as the set coordinate of the mouse point then convert these screen points to world points and return this vector as result okay and now we can use this method to set the position of the object when it is direct with the mouse and also to calculate the drag offset when the object is selected by pressing the left mouse button alright then I attached the script to the rock for example and when I start the game you can see that we can drag the rock now with the mouse okay this works we can do the same for the stone that has a box Collider attached and it could also be really interesting when we attach rigidbody components to the game objects okay that's a quite nice result I copied the script to the comments below so that it can use it in your own projects I hope you liked this short tip and if you do then don't forget to subscribe to my channel and if you want to support me consider being my patron this would really help me creating more tutorials like this thanks a lot guys for watching thanks for your support in advance and I see you in the next one
Info
Channel: Jayanam
Views: 140,722
Rating: undefined out of 5
Keywords: unity tutorial drag gameobject, unity C# script drag, unity drag gameobject, unity script drag, unity rigidbody gameobject, unity rigidbody tutorial, unity c# rigidbody, unity pick gameobject, unity select gameobject, unity tutorial, unity scripting tutorials, unity beginner tutorials, unity c# basics, unity c# tutorials, game development, unity translate rotate, unity gameobject onclick, unity drag object, unity drag and drop 3d, unity pick up objects, jayanam
Id: 0yHBDZHLRbQ
Channel Id: undefined
Length: 3min 45sec (225 seconds)
Published: Sun Nov 25 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.