Get input mouse position in 2D?

I want to create a gameobject on mouse click position. This is what I did. It does create the object, but it’s z-position is something like -9 and thus it can’t be seen on the camera.Though it appears fine in the hierarchy and Scene window.

Ray ray = Camera.main.ScreenPointToRay(mousePos);

	if(Input.GetMouseButtonDown(0)){
		GameObject crate = (GameObject)Instantiate(blackBox, ray.origin, Quaternion.identity);
	}

Is there any way in which I can set the z-position to 0. I know there is a Ray2D class and I tried doing this(below), but it says "Cannot implicitly convert type ‘UnityEngine.Ray’ to ‘UnityEngine.Ray2D’ "

Ray2D ray = Camera.main.ScreenPointToRay(mousePos);

any ideas? Thanks in advance

shorter: var renderer = GetComponentsInChildren().FirstOrDefault(r => r.tag == "Your Tag");

1 Answer

1

GameObject crate = (GameObject)Instantiate(blackBox, Vector3( mousePos., mousePos.y, 0 ), Quaternion.identity);

That should do it