- Home /
Ray: finding out x and z coordinates where it intersects y = 0
Hello, I'm trying to do an RTS style building placement where the blueprint of an object follows the mouse while dragging along the ground.
The catch is that this is that I'm effectively placing the terrain, there may or may not be something in that location, so collision detection isn't really an option.
I need a reasonably simple way to find out where my ScreenPointToRay ray intersects y=0 but my vector math is rusty to non-existant. Is there a function or set of functions in Unity that would make this simpler than breaking down the two vectors I have into individual numbers and coding a wall of geometry?
what I currently have:
void Update()
{
Ray ray = Camera.main.ScreenPointToRay(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0));
???
temp.transform.position = new Vector3((int)???,0,(int)???);
}
Answer by aldonaletto · Jun 19, 2012 at 03:10 AM
You can create a horizontal logic plane at y=0, then use a Plane.Raycast to find where the ray hits the plane and get the exact position with Ray.GetPoint:
void Update(){
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
// create a plane at 0,0,0 whose normal points to +Y:
Plane hPlane = new Plane(Vector3.up, Vector3.zero);
// Plane.Raycast stores the distance from ray.origin to the hit point in this variable:
float distance = 0;
// if the ray hits the plane...
if (hPlane.Raycast(ray, out distance)){
// get the hit point:
temp.transform.position = ray.GetPoint(distance);
}
}
That is incredibly elegant and does exactly what I was ai$$anonymous$$g for, thank you.
Thanks, works great with Touch as well. Here's how I did it:
void Update(){
if(Input.touchCount > 0) {
Touch touch = Input.GetTouch(0);
switch(Input.GetTouch(0).phase) {
case TouchPhase.Began:
$$anonymous$$akeSphere(touch);
break;
case TouchPhase.$$anonymous$$oved:
break;
case TouchPhase.Ended:
break;
}
}
}
void $$anonymous$$akeSphere(Touch touch){
Ray ray = Camera.main.ScreenPointToRay(touch.position);
// create a plane at 0,0,0 whose normal points to +Y:
Plane hPlane = new Plane(Vector3.up, Vector3.zero);
// if the ray hits the plane...
float distance = 0;
if (hPlane.Raycast(ray, out distance)){
// get the hit point:
Vector3 location = ray.GetPoint(distance);
GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
sphere.transform.localScale*=5;
sphere.transform.position = location;
}
}
Answer by Wolfram · Jun 19, 2012 at 02:58 AM
If it's really just y=0 you are interested in, make a plane with collider that covers your whole area, remove the MeshRenderer, so that it's an invisible collider, and place it at y=0.
Then you can simply raycast at this particular collider, using Collider.Raycast(...), and it will always return the intersection with y=0 at that point.
I had just finished juryrigging a plane to follow underneath the camera when Aldo's answer came up. Both answers gave the same result but his is more scalable. It appears I can't flag multiple answers though, so his has to be it=(
Answer by randomdragon · Jan 24 at 11:39 AM
Knowing the x1, to get the y1 and z1, simply use the direction vector v= ( x, y, z) :
dx = x1 - x;
dy = v.y * dx/v.x;
dz = v.z * dx/v.x;
y1 = y + dy;
z1 = z + dz;
Your answer

Follow this Question
Related Questions
Casting a Ray in the direction of the movement 1 Answer
How do I keep functionality of LookAt script whilst using it in a 3dGUI camera layer? 0 Answers
Question about Rays and why getPoint function 1 Answer
Math Question : How to Get a Point on a Ray which is EXACTLY 5 units away from Vector3.Zero? 5 Answers
Raytrace Ignore the x axis ? Or Am I just a noob ? x') 0 Answers