Capturing 3D position with mouse

Hi everyone! New to the forum. Thanks for all having knowledge that I don’t.
I know it’s common to use Camera.main.ScreenToWorldPoint(point) to get a world position where point can be Input.mousePosition or adding a “z” value to it, or to use Camera.main.ScreenPointToRay(Input.mousePosition).GetPoint(distance) to do the same thing.
Here is my problem. I’m doing something like 3D modeling so I need to capture a 3D point with mouse. However I don’t know how to decide the value of “z” (or distance) from the camera. I wonder if someone here can help me with the strategy.

If the z position is based on something in the scene, you can use that. Otherwise, I think it’s just up to you where it will go.

Sorry, maybe I didn’t understand your question very well.

For example, if I’m drawing a line from scratch, the only point I can base on is (0, 0, 0). If I draw the line from (0, 0, 0) then how to decide which plane (XY, YZ, XZ or else) the end point is on?

Ah okay, well I’m not sure. Maybe you have one set to default but toggles to switch to others?

Maybe someone else will have some other ideas. :slight_smile:

Okay, but thanks anyway.:slight_smile:

If you are trying to use the mouse button to allow the user to select a point in world space, your are doing it right with the GetPoint(Distance). However, if you want the user to click on a GameObject to select it or to select a point on the gameObjects mesh, you can use Physics.Raycast to send a ray that will hit a vector point in the mesh and then use RaycaastHit to get the info of the Vector3 RaycastHit.Point.

Hope that is what you were trying for.

Basically this is not a programming question, it’s a design question. A two-dimensional input device (like a mouse) cannot specify a 3D point. So it’s up to you, the designer, to decide how that third coordinate (distance from the camera) should be chosen. You can do whatever you want. Have a reference plane that you raycast against? Yep, that’ll work. Require the user to click on the surface of an object? Sure. Keep the new point in line with the previous two points? Sure, if they’re all colinear (otherwise, pick the nearest colinear point). Make the user actually type X, Y, and Z? OK. Restrict the user to only orthogonal views (XY, XZ, or YZ) and input/display the third coordinate in an edit field? You bet. Let the user change the ray distance using the scroll wheel? Why not?

It’s up to you. If you really don’t know what you want, then I recommend you play around with some 3D modeling apps (or with Unity itself) to see how they handle this.

Thank you very much, JoeStrout. As you say, it’s a design question. My boss decide to imitate the way that SketchUp handles this problem. However, I have problems with inferring first points (like my example at #3). Do you know about how SketchUp handles this? Or do you have any idea about that example? Thanks again.

Sorry, I haven’t used SketchUp too much. I don’t know how it handles it. As for your example, I think you just pick some arbitrary plane, and raycast into that.

It doesn’t matter. Anyway, your former suggestion is quite helpful. Many thanks!

1 Like