Mouse Position to World Position on Ground Plane

I’m trying to convert the mouse position to a world position situated on a ground plane at y = 0. I’m doing it with the following code, but for some reason my cursor and the resulting debug sphere are not in the same location, and I assume this is indicating that something is wrong with how I’m translating screen coordinates to world space.

Plane ground = new Plane(Vector3.up, 0f);

// ...
public Vector3 MouseToWorld()
{
	Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

	float distToGround = -1f;
	ground.Raycast(ray, out distToGround);
	Vector3 worldPos = ray.GetPoint(distToGround);

	Debug.Log("Mouse at: " + worldPos);
	return worldPos;
}

And then in OnDrawGizmos:

// Draw world position
Gizmos.DrawWireSphere(MouseToWorld(), 0.1f);

I have placed small reference objects in the scene at y = 0 (on the ground plane) and when mousing over them, the gizmo is not drawn in the expected location, but is a unit or two out of place.

I’m not sure about your plane constructor. Try this:
Plane ground = new Plane(Vector3.up, Vector3.zero);.

Same problem, unfortunately :frowning:

Here’s an image illustrating the problem:

The area highlighted in red is the scene origin (there’s a small cube there that’s not visible in the image because I covered it in the more visible red outline).

Are you trying to display the gizmo in the editor? You’ll want to use Camera.current instead of Camera.main if that’s the case.

Camera.current seems to be using the scene view camera. At any rate, it also gives completely unexpected and unpredictable results. I also tried replacing Camera.main with an explicit reference to the game camera, but it gave the same results as the first example.

I started a new scene, new scripts, new everything: It works as expected. I’ll see if I can identify what might have been causing the problem.

I spoke too soon. It worked in one scene (or appeared to) and now I’ve tried it again, it doesn’t.

I’ve included a package containing a very basic example demonstrating the problem, if anyone could be so kind as to take a quick look and see if you can identify what I’m doing wrong.

As far as I can tell, this code should successfully draw a small wire sphere corresponding to the position of the mouse cursor projected onto the ground plane. However if I hover over the objects in the scene (also on the ground plane) the gizmo is drawn with a large offset.

Download the test package here.

Pretty weird, the sphere is appearing directly under my cursor in the Game view, with Gizmos enabled. Looks like it’s something on your end? :face_with_spiral_eyes:

Well that wasn’t the result I was hoping for :frowning: any idea what it could possibly be? No wonder I was having difficulty troubleshooting it :frowning:

Okay so I moved to another PC and tested it and I had the same problem. However I wasn’t viewing the Gizmo in the game view, I was viewing it in the scene view. So I turned Gizmos on for the game view and it appears to work find. Which raises another question: Why the heck is the Gizmo in an entirely different location in the Scene view?

Here’s another image that hopefully more clearly demonstrates the issue. This is tested on PC #2. The gizmos in the scene view don’t seem to be drawing where they are supposed to. Is this a bug, or working as intended in a way I don’t understand?