- Home /
Is there a way to get mouse position in 3D space at a given y-value?
I've got one solution to this problem by performing a raycast on a floor plane set at Y = 0 like so:
RaycastHit hit;
if(floor.Raycast(Camera.mainCamera.ScreenPointToRay(Input.mousePosition), out hit, 100))
{
Vector3 hitPos = hit.point;
}
but I'm wondering if there's a more efficient way of doing it without a raycast or a plane if I can specify Y as a constant? This may be the best solution out there, but I'm curious if anyone has a better one before I call it.
Thanks!
Answer by Zerot · Nov 12, 2012 at 11:41 AM
A slightly cheaper raycast would be against a Plane(assuming that currently floor is a collider).
Example:
Plane plane = new Plane(Vector3.up,0);
float dist;
Ray ray = Camera.mainCamera.ScreenPointToRay(Input.mousePosition);
if (plane.Raycast(ray, out dist))
{
Vector3 point = ray.GetPoint(dist);
}
Edit: I was erroneously assu$$anonymous$$g you were suggesting creating a Plane collider in my previous comment... this is actually a good response, thanks!
if i do it like that i get an error: NullReferenceException UnityEngine.Camera.ScreenPointToRay (Vector3 position)
If you get a null reference exception from running this code, it's because your camera doesn't have the tag 'Camera'.
Answer by Seth-Bergman · Nov 12, 2012 at 06:42 AM
You could basically just drop the raycast:
Camera.main.ScreenToWorldPoint(Input.mousePosition)
but if you are using a downward-pointing camera, as it sounds, you will probably need to swap y and z coordinates.. since "z" is the distance from the camera, i.e.:
var myTempPoint : Vector3 = Camera.main.ScreenToWorldPoint(Input.mousePosition);
var myWorldPoint : Vector3 = new Vector3(myTempPoint.x,myYConstant,myTempPoint.y);
Hi Seth,
thanks for the response. If I recall correctly, ScreenToWorldPoint would require me to pass in a Vector3 with the X and Y values set to screen space, and Z value set to distance of point of interest from camera. This would work well if I were using a downward-pointing camera since the Z would be constant, but I'm actually using a camera angled at 45 degrees to achieve an isometric 3rd-person feel. In order to set the distance of the point I'm interested in in the proper world space from that angle, I would ironically need to perform a raycast. Are my assumptions correct, or am I missing the gist of your solution?
Just to clarify, when I mentioned setting a constant Y, I meant in world space, not in camera local space.
p.s. I tried passing Input.mousePosition straight into ScreenToWorldPoint as suggested, but since Input.mousePosition's Z value (distance from camera) is 0, ScreenToWorldPoint will return the same coordinates regardless of where the mouse is on the camera - the camera's position in world space
H$$anonymous$$$$anonymous$$, I was assu$$anonymous$$g an orthographic camera (among other wrong assumptions).. When you put it like that, I would say yes what you want is a Raycast!
Sorry if my original inquiry was a bit ambiguous, and thanks for proposing a possible solution even after the fact. I'll keep my raycast implementation for now, until such time a better solution comes around, if it ever will.
What's the protocol for marking an answer correct? I'm new to using systems like this. Although the original answer didn't exactly solve my problem, I'd like to give you credit for trying and subsequently confir$$anonymous$$g my implementation.