THIS CONTENT DOWNLOAD SHORTLY

Objective

Main objective of this blog post is to give you an idea about how to use Pick and Place Object at Mouse Position in Unity.

 

You will get Final Output: 

instantiate-at-mouse-position

 

Step 1 Basic Information

You can instantiate multiple clones of given Game Object or Object Prefab using the function:

Instantiate (Object Target, object’s position, object’s rotation) Or
Instantiate (Object Target)

If you want to instantiate a game object at position where clicked on the surface is occurred, then have to first find that world point of tabbed place.

You can do this using two ways:

  1. If you are making shooting game, to find where the object is hit, can use hit.point
  2. You can use mouse position that can easily be converted into world position.

Here, using mouse position we will place the object in the world position. To get the mouse position, we can use the function:

Input.mousePosition

This function returns the position that is in pixels. So, we need to convert it into the World position.

To convert the mouse position into the World position, we will use the function:

Camera.main.ScreenToWorldPoint

This function will convert the screen transform position into World position. Now you can use this position to place object at where mouse click is done.

Now, by using this functions we will create a short demo of it.

 

Step 2 Demo Project

Open unity and go to File >> New Project

Give the name InstantiateObjectAtSpace and select 2D from drop-down menu and press create button.

Create a scene with main camera and a Game Object, which is used as a target Object for making a multiple clones of it.

You can also create a canvas and a panel and set any image as a background. (Optional)

Here, image can also be used as a target object. For this, we need to convert image into 2D sprite and after converting we can use that image as a Game object in the scene.

Now, Apply this Below C# Script on Camera and pass the reference of the target Object which we want to instantiate.

InstantiateAtMousePosition.cs

using UnityEngine;
using System.Collections;
 
public class InstantiateAtMousePosition : MonoBehaviour {
 
    Vector3 mousePosition,targetPosition;
 
    //To Instantiate TargetObject at mouse position
    public Transform targetObject;
 
    float distance=10f;
 
    // Use this for initialization
    void Start () {
    
    }
    
    // Update is called once per frame
    void Update () {
 
        //To get the current mouse position
        mousePosition=Input.mousePosition;
 
        //Convert the mousePosition according to World position
        targetPosition=Camera.main.ScreenToWorldPoint(new Vector3(mousePosition.x,mousePosition.y,distance));
 
        //Set the position of targetObject
        targetObject.position=targetPosition;
 
        //Debug.Log(mousePosition+"   "+targetPosition);
 
 
        //If Left Button is clicked
        if(Input.GetMouseButtonUp(0))
        {
            //create the instance of targetObject and place it at given position.
            Instantiate(targetObject,targetObject.transform.position,targetObject.transform.rotation);    
        }
    }
}
 

Step 3 Scripting

Here, Left Mouse Button is used to Place Object. While placing the object on space, keep pressing Left Mouse Button Down to move the position of the object.

I hope you find this blog post very helpful while using Pick and Place Object at Mouse Position in Unity. Let me know in comment if you have any questions regarding Unity. I will reply you ASAP.

Got an Idea of Unity Game Development? What are you still waiting for? Contact us now and see the Idea live soon. Our company has been named as one of the best Unity Game Development Company in India.

I am a game developer trying to explore my knowledge by implementing new concepts so that games can be made more creative and innovative.

face mask Belial The Demon Headgear Pocket Staff Magic