instantiate object at mouse position

pretty simple I think, I just want to instantiate the object upon mouse click at the position of the mouse.

var box : Transform;

function Update () {
if (Input.GetMouseButtonDown(0))
Instantiate(box,Vector3 (Input.mousePosition.x,0,Input.mousePosition.z), Quaternion.identity);

It's instantiating, but not at the mouse position.

Here a C# code i use, it will try to cast a ray and instantiate object at correct positon, if ray doesnt hit anything it uses ScreenToWorldPoint as mentioned in other’s answers.

 	Vector3 mousePos=new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0f);
    if(Input.GetMouseButtonDown(0)) {
    	Vector3 wordPos;
    	Ray ray=Camera.main.ScreenPointToRay(mousePos);
    	RaycastHit hit;
    	if(Physics.Raycast(ray,out hit,1000f)) {
    	} else {
    	//or for tandom rotarion use Quaternion.LookRotation(Random.insideUnitSphere)

Input.mousePosition is in screen coordinates (in pixels). To get a position in world coordinates you need to use Camera.main.ScreenToWorldPoint(Input.mousePosition). Input.mousePosition is also a 2 dimensional position (because your screen is flat). To get a correct and meaningful world position you will also have to specify a z coordinate to describe how far in front of the camera the world point should be. So if you have a camera looking straight down onto a plane and the camera has a distance of 10 to the plane you would set the z coordinate to 10 to instantiate something on that plane.

One possible reason is that you may be reading in the mousePosition property of the Input class incorrectly. You can read this link right here and be enlightened, but here's the gist of it:

  • mousePosition may be a Vector3, but the info you get from it is actually just a 2-coordinate system. That is, the x and y of the mouse position on your screen. They're basically mapped to the x, and y of the Vector3 return by mousePosition, while z has a value of 0 (if I'm not mistaken).

You can check out this here link on the ScreenToWorldPoint function of the class Camera to help you out. However, I feel happy today so I might as well give you something to get started on your solution :D

  1. What you wanna do is to first get the mouse position in world space:

    Vector3 mousePositionInWorld = Camera.main.ScreenToWorldPoint(Input.mousePosition);

  2. Now that you've basically got your position in the world, you can try instantiating the object using mousePositionInWorld. However, you may find that it might still be instantiating a little ways off, particularly your Y coordinate. What you'll wanna do is set it correctly like so:

    mousePositionInWorld.y = 0 // in your code, you setting it to zero so let's do that

  3. Try again and the object (box) should be instantiating at the position where your mouse is with its y value consistently staying at 0.

you set the y value at 0.0f , are you not just getting the object in the screen at a position you just don't see? It's pretty lame, but those things happen ;)

So if I understand the comments above correctly the code should look something like this?

enter code here
var thePrefab : GameObject;

function Start () {

if (Input.GetMouseButtonDown(0))
var mousePositionInWorld = Camera.main.ScreenToWorldPoint(Input.mousePosition);


However, this doesn’t seem to be working either. Perhaps I misunderstood the instructions above. I am actually trying to solve for a similar problem.

It seems to set the animation on start to 0,0,0