- Home /
The question is answered, right answer was accepted
click on screen to get the coordinate on the ground(x,z plane)
Hello, I am making 3D map. there is a ground on the x,z plane. I want to click on the ground, and to know which point on the ground i am clicking on. I am looking for the clicking point's coordinate in the worldspace(x,0,z). I am stuck in this problem for a week, I need your help. Thanks in advance. I
Answer by markpdolby · Jul 03, 2013 at 08:57 AM
void Update(){
Ray ray =Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if(Physics.Raycast(ray, out hit, 100, LayerMask.NameToLayer("Terrain")))
{
Debug.Log(hit.point);
}
}
Use a ray cast from the mouse position onto the ground. the two things you might need to change are the 100 which is maximum distance the ray will travel and the layer name to whatever layer your ground is on. See the docs for more detail: http://docs.unity3d.com/Documentation/ScriptReference/Physics.Raycast.html
Layer$$anonymous$$ask.NameToLayer("") didn't work for me.
I had to use Layer$$anonymous$$ask.Get$$anonymous$$ask("")
//get the mask to raycast against either the player or ground layer
int l$$anonymous$$ask = Layer$$anonymous$$ask.Get$$anonymous$$ask("Player", "Ground");
//player only
//int l$$anonymous$$ask = Layer$$anonymous$$ask.Get$$anonymous$$ask("Player");
Follow this Question
Related Questions
mouse clicking on the plane and drag to other position 1 Answer
Firing projectile towards mouse position from player... 0 Answers
Player to look at 0 Answers
how to make the object position related to the screen 2 Answers
Spawn Objects Where i click 2 Answers