How To Do Drag And Drop Game Objects With The Mouse In Unity
- Agnesh Pipaliya
- Jul 5, 2022
- 288
You have played many games on your smartphone, tablet, or laptop. And you have observed that we need the mouse to move a game object from one place to another.
To drag and drop an object, the mouse is a common mechanic that is used differently in various games. For instance, clicking and moving inventory items in the game world.
Here is the basic guide to learning to drag and drop game objects with a mouse.
Step - 1: Create Project
First of all create a brand new unity 3D project and set the scene as shown below. Take an empty game object and name it as MyDragAndDrop and create some other game objects like sphere, cube, etc.
Step - 2: Create Script
Now create new C# script and give name what you like, in my case i am just save this with names like “MyDragAndDrop.cs”.
Step - 3: Click Detection
First of all you need to enter the code required to detect the gameobject when we clicked on it.
Step - 4: Update Method
Now just copy this code and paste in to the Update method.
Step - 5: Apply Script
Apply this script onto “MyDragAndDrop” GameObject.
Step - 6: Testing Demo
Now run the scene and drag and drop gameobject that you want to move. You will see this is working fine.
Conclusion
This beginner's guide will help you develop a simple drag-and-drop game object with a mouse. And you will be able to create this feature on the Unity platform.