Drag object Script in Unity3D


2Let’s logically think this through, what all smaller pieces in this problem can be broken down.

  1. Placing the object in the scene.
  2. Make the object clickable as in it should be registered when we click the mouse on it: OnMouseDown()
  3. The object should follow the mouse pointer with some or no offset: OnMouseDrag()

We don’t need any script for 1st step.

The mouse pointer works in a Screen Space which is a 2D space defined in pixels whereas the gameObject lies in a World Space which is 3D space where everything from camera to lights is placed.

Unity defines Screen Space starting from bottom-left of the screen is (0,0) to the right-top is (pixelWidth,pixelHeight). The z position is in world units from the camera.

Screen Space
The game object co-ordinates in screen space

To get the gameObject to interact with the mouse cursor we need to bring its co-ordinates into the co-ordinate system in which the mouse cursor operates i.e. we need to transform the world co-ordinates of the gameObject to Screen Space co-ordinates. How to do it? Unity has a custom made method called WorldToScreenPoint()

private Vector3 screenPoint;
private Vector3 offset;

void OnMouseDown()
screenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position);

offset = gameObject.transform.position – Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));


To obtain the z co-ordinate of game object in screen space we wrote this line of code

screenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position);

screenPoint.z will give the z co-ordinate which will be used as the z co-ordinate of the mouse input as well.

Input.mousePosition gives screen space co-ordinates of the mouse cursor which needs to the converted to the world space to perform the offset calculations. Here we use the screenPoint.z, the z screen space co-ordinate of game object as the z screen space co-ordinate of the mouse cursor.

void OnMouseDrag()
Vector3 cursorScreenPoint = new Vector3 (Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
Vector3 cursorPosition = Camera.main.ScreenToWorldPoint (cursorScreenPoint) + offset;
transform.position = cursorPosition;

When we drag the mouse Unity needs to take the position of the mouse cursor and update the position of the game object in real time hence whatever the piece of code we write for dragging of the mouse it will be executed as long as the mouse is being dragged and has to be written inside OnMouseDrag().

First we store the position of the cursor using cursorScreenPoint 

Vector3 cursorScreenPoint = new Vector3 (Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);

This line of code will give us the position of the mouse cursor but only in the Screen Space, later we will convert it to World Space.

Using this line of code we convert screen space co-ordinates to World Space co-ordinates and add the above calculated offset to it.

Vector3 cursorPosition = Camera.main.ScreenToWorldPoint (cursorScreenPoint) + offset;

Finally to update the position of our game object based on the position of the mouse cursor along with some offset we add this line of code.

transform.position = cursorPosition;

There you go, you have successfully completed a script that let’s you to drag a game object.










Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s