

How to convert the mouse position to world space in 3D.How to convert the mouse position to world space in 2D.Here’s what you’ll find on this page: Methods for getting the world position of the mouse on the screen (using Screen To World Point) If you prefer to learn by watching, try my video on how to convert the mouse position to world space in Unity, or you can continue to the full article below. In this article, you’ll learn the different methods for finding the mouse position in 2D and 3D Scenes, find out about some common pitfalls to avoid and see examples of how to use the mouse to interact with other objects in the game. Options include using Camera.ScreenPointToRay with Physics.Raycast for checking against 3D colliders or Physics.Raycast2D for 2D games.

Almost all of them involve firing a Ray into the Scene from the mouse’s world position to collide with objects and retrieve a reference to them. To interact with Game Objects using the mouse, there are several methods available. If you don’t, no movement will be detected. When Using a 3D Perspective Camera you must set the Z value of Input.MousePosition to a positive value (such as the Camera’s Near Clip Plane) before passing it into ScreenToWorldPoint. To calculate the mouse position in world space, use Camera.ScreenToWorldPoint with Input.mousePosition, to get a Vector3 value of the mouse’s position in the Scene. How do you get the mouse position in world space in Unity? Whatever it is, you’re going to need to translate the mouse’s position on the screen, which is measured in pixel coordinates, into a real position in the world that Game Objects use. Perhaps you want to make another object face the position of the mouse or fire something towards it. It’s a property of the Input class so, to access it from a script, all you need to do is use Input.mousePosition, which returns the position of the mouse, in pixels, from the bottom left of the screen.īut what if you want to actually do something with the mouse position in the game world, such as click on a Game Object, or drag something around the screen.

In Unity, getting the mouse position on the screen is fairly straightforward.
