Mouse Position Woes

I’ve done some serious googling related to placing objects on screen where the mouse is located. Currently I’m using this function, which is called in Update()

blah is a GameObject
point1 is a Vector3

void ScreenClick()
	{
		if(Input.GetKeyDown(KeyCode.Mouse0))
		{
				point1 = mainCam.ScreenToWorldPoint(Input.mousePosition);
				point1.z = 0;
				Debug.Log (point1);
				Debug.Log (Input.mousePosition);
				Instantiate(blah, point1, transform.rotation);
		}
	}

When I left click, it spawns object’s at the camera’s location [0,1,-10], regardless of where I click…(I set the z to 0 in the next line so the object always spawns in front of the camera @ [0,1,0].

What am I doing wrong? In the debug, point1 is always (0,1,0) and the mousePos is always correct/changing…but the ScreenToWorldPoint does not seem to be functioning.

setting z to 0 is not going to put it in front of the camera unless the camera z is below 0.

z should probably be mainCam.position.z + 10 (or maybe its minus… not sure off the top of my head)…

It is, the camera is -10…that’s not the problem though, I just wanted to clarify why I set it to 0. The problem is that the objects are always spawning at (0,1,0) when it should be (x,y,0) where x and y are the current mouse position in world space.

Firstly, you’re setting z to 0 in world space, not the local space of the camera.

Secondly, where are the colliders in your scene? ScreenToWorldPoint uses a raycast, so if there’s a hidden collider/trigger in front of your camera it may be hitting that, and if there’s not a collider on the thing you expect to hit it’ll be ignored.

Thirdly, how is your camera set up? I can’t remember what I was doing the other day, but I recall having similar results with an ortho camera… ah yes. I was moving something to the clicked position, but the thing I was moving was clickable itself, so every frame it moved closer to the camera by its radius (because every frame the mouse was down I was “clicking on it” and moving it to the point on its collider where I’d clicked).

I want a z of 0. The game is a side scroller, so all the objects have the same z value.

There are no colliders in the scene, save a cube and a sphere. Clicking on their locations does not alter where the objects is instantiated. After reading your post I created a plane with a mesh collider (disabled the mesh renderer) and put it in the scene. However, this did not fix the issue, it still spawns @ (0,1,0) everytime.

I’m using the default perspective camera.

Thanks for all the responses! Much appreciated!

EDIT: It makes sense now as to why I need a collider in the scene. The mouse to world position could change depending on where the collider is. I have a plane with mesh collider now encompassing the whole scene, and it does not seem to have changed anything. Do I need to use a simple collider?

maybe try using ViewportToWorldPoint

Solved

Thanks again everyone for the pointers. You helped me out a bunch. I solved this by setting the z to the camera plane.

Example code:

mousePos = Input.mousePosition;
mousePos.z = (Camera.main.farClipPlane-2);
point = Camera.main.ScreenToWorldPoint(mousePos);
Instantiate(blah, point, transform.rotation);

You can also use the nearClipPlane if you’d like. Anyway, once I used this, everything functioned correctly. Thanks again, I hope this helps someone in the future who has a similar problem.