Mouseposition in Worlf

Hi,

I try to instantiate a GameObject at the mouse´s cursor position. Because its a FPS the mousepos is in the middle of the screen.

I use the following code to realize it:

			RaycastHit hit = new RaycastHit();
			Vector3 pos = new Vector3 ();


			if(Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit))
				pos = hit.point;

			float heightOfTerrain = 0.0f;
			Terrain terrain =  (Terrain)GameObject.FindObjectOfType(typeof(Terrain));
			heightOfTerrain = terrain.SampleHeight (pos);
			pos.y = 1.0f + heightOfTerrain;

But now my problem is, that sometimes the Mouse does not hit the terrain. The mouse hits the GameObject I want to place. What happens is logical: the GameObject moves in my direction. Because I hit the gameobject with my Cursor I get wrong coordinates.

So the question is: Is there any way to ignore Gameobjects when i want to get the coordinates of the mouse?

Or perhapps i do it the wrong way. Is there any better way to place a gameobject?
I want the user to select a wall for example and then he can place the wall in the world.

You can assign GameObjects to a layer. Physics.Raycast() has a call that allows you to specify which layers your Raycast should hit, and which layers it should ignore.

Eg: In our game, enemies are on a different layer than friendly characters. This makes it super easy to Raycast only enemies.