Course contentsShow
← Unity Game Development
Lesson 106 of 8282. Scripting and LifecycleFree lesson

Converting Screen Coordinates to World Space

Learners will translate mouse screen positions into world-space coordinates to allow objects to follow or interact with the cursor.

In this lesson, you will learn how to bridge the gap between your monitor’s 2D pixel grid and your game’s 3D environment by converting mouse screen positions into world-space coordinates.

The Problem: Two Different Worlds

Your game operates in two distinct coordinate systems. The Screen Space is a flat, 2D grid based on your monitor’s resolution (where 0,0 is the bottom-left corner). World Space is the infinite 3D environment where your GameObjects live. When you move your mouse, Unity provides coordinates in pixels, but if you try to apply those values directly to a 3D object’s transform.position, the object will disappear or snap to the camera’s origin.

The Solution: Using Camera Projection

To make an object follow your mouse, you must ask the Camera to translate the screen’s "flat" point into a "depth-aware" point in the 3D world. We use the Camera.main.ScreenToWorldPoint() method to achieve this.

Because the screen has no "depth" (it's just an X and Y), we must provide a z value to tell Unity how far into the 3D world the point should be. If you are working in a 2D game, you might set this z to a specific distance from the camera.

Here is how you implement it:

void Update()
{
    // 1. Get the mouse position from the Input manager
    Vector3 mousePos = Input.mousePosition;

    // 2. Set the Z distance (how far from the camera)
    // 10 units is common for 2D games with a camera at -10
    mousePos.z = 10f; 

    // 3. Convert the screen pixels to 3D world units
    Vector3 worldPosition = Camera.main.ScreenToWorldPoint(mousePos);

    // 4. Apply the position to the object
    transform.position = worldPosition;
}

By providing that z distance, you create a "plane" in the 3D world where the mouse interaction occurs. Without defining this depth, the function would return the camera's exact position, rendering the conversion useless. Once you have the converted worldPosition, you can treat it just like any other coordinate in your scene.

Key Takeaway: Use ScreenToWorldPoint combined with a defined z depth to accurately project 2D mouse coordinates into your 3D world space.