Mouse position and the z-axis

Hi folks,

first post on the forum. Have just started developing on Unity, and really enjoying it so far. But wondered if anyone could help me with something I’ve been scratching my head over so far…

I have a scene where the camera is slightly raised, so I guess you have a kind isometric view.

I want to move an object along x (have no problems with this), and also be able to move it towards and away from the camera, i.e. along the z-axis.

I have kind of accomplised this by using the mouse position of y, working out that half the screen is filled by the floor, and just doing some calculations as to a rough estimate of where on z corresponds to y.

This works after a fashion, but is a bit messy. It seems there should be a much better way to get depth from a button click somewhere on a plane/floor.

Would greatly appreciate any ideas.

Thanks!

Hi and welcome to this great comunity.

I don’t know what you are doing now, but I think that you should use the method Camera.ScreenToWorldPoint using the mouse position.

For more information you can visit a previous thread that talk a bit about that:

http://forum.unity3d.com/viewtopic.php?t=9980

You can also throw a ray using the method Physics.Raycast (in combination with the method Camera.ScreenPointToRay).

Note that the Raycast method returns to you (as an output parameter) a RaycastHit with the information of the ray collision.

thanks for the reply. I had a look through the other thread, and looked at the ScreenPointToRay function, and I don’t understand how it captures the z-axis.

I do use screen to world point - and the object I move works fine along the x-axis. but because mouse position only returns an x and y, I have no idea how to get distance/depth.
The ScreenPointtoRay also seems to say that z is ignored.

any ideas?

oh, and thanks for the welcome, btw. :slight_smile:

You are never going to move the mouse along the Z axis. Mice don’t work that way.

What he was saying was that you could approximate the mouse’s position in the world by raycasting from where it was and using the point that it intersects.

Most games, like Black and White, use that technique while hiding the mouse cursor and generate a 3D cursor in the world where the raycast hit the ground.

ok, think I get it - I could raycast from the mouse position straight out, and catch the point where it intersects the floor/plane, and use that.

Thanks for the help guys.