How to get mouse position in Unity

Unity input system returns the mouse position with a single command. But, the mouse position cannot be directly used in the game. Unity returns the mouse position in pixels, you need to convert it into world space coordinates to use it in your game. In this tutorial, we will see how to get mouse position in Unity and use it in different scenarios of your game.

Get mouse position in old Unity input system

Step1: Get Mouse position

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour
{
    void Update()
    {
        Vector3 mousePos = Input.mousePosition;            
    }
}

Mouse position will be returned in the x and y axis based on the width and height of the screen.

Step2: Convert mouse position to world Position(both 2D and 3D)

Mouse position obtained in the previous has zero as value for the z axis. When converting it to world point this may cause the object to vanish from the camera view. To avoid this we give the cameras clip plane distance as the z value.

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour
{
    void Update()
    {
        Vector3 mousePos = Input.mousePosition;   
        mousePos.z=Camera.main.nearClipPlane;
        Vector3 Worldpos=Camera.main.ScreenToWorldPoint(mousePos);  
        Vector2 Worldpos2D=new Vector2(Worldpos.x,Worldpos.y)
        //Worldpos2D is required if you are making a 2D game 
       
    }
}

Step3: Use the mouse position in your game

Let’s instantiate the player prefab in the mouse position

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour
{
    Public gameObject Player;
    Bool player_dead;
    void Update()
    {
        Vector3 mousePos = Input.mousePosition;   
        mousePos.z=Camera.main.nearClipPlane;
        Vector3 Worldpos=Camera.main.ScreenToWorldPoint(mousePos);  
        If(player_dead)
        {
             Instantiate(Player, Worldpos, Quaternion.identity);
        }
       
    }
}

using mouse position in a 2D game

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour
{
    Public gameObject Player2D;
    Bool player_dead;
    void Update()
    {
        Vector3 mousePos = Input.mousePosition;   
        mousePos.z=Camera.main.nearClipPlane;
        Vector3 Worldpos=Camera.main.ScreenToWorldPoint(mousePos);  
        Vector2 Worldpos2D=new Vector2(Worldpos.x,Worldpos.y)
        If(player_dead)
        {
             Instantiate(Player2D, Worldpos2D, Quaternion.identity);
        }
       
    }
}

Get mouse position in New Unity input system

Step1: Install the new Input system

  1. Go to Window>Package Manager.
  2. Search for Input System and click install.
  3. You will see a pop up to enable backends click YES.
  4. The editor will restart and the old input system will be disabled.

Step2: Modify the above code to suit the new input system

  1. Add using UnityEngine.InputSystem.
  2. Change Input.mouseposition to  Mouse.current.position.ReadValue()
using UnityEngine;
using System.Collections;
using UnityEngine.InputSystem;

public class ExampleClass : MonoBehaviour
{
    Public gameObject Player;
    Bool player_dead;
    void Update()
    {
        Vector3 mousePos = Mouse.current.position.ReadValue();   
        mousePos.z=Camera.main.nearClipPlane;
        Vector3 Worldpos=Camera.main.ScreenToWorldPoint(mousePos);  
        If(player_dead)
        {
             Instantiate(Player, Worldpos, Quaternion.identity);
        }
       
    }
}

Hope it was helpful. If you have any other questions on getting mouse position, please leave it in the comment box below.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.