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.