How to Create a Smooth Mouse Movement in Unity

Smooth mouse movement is an essential aspect of game development that helps to improve the overall user experience. By implementing smooth mouse movement, you can make your game's camera or player controls feel more fluid and responsive, resulting in a polished and immersive gameplay experience. In this tutorial, we will walk through how to set up smooth mouse movement in Unity, with step-by-step instructions and code examples in C#. We'll also discuss the possible reasons why you would want to implement this feature in your games.

Why Implement Smooth Mouse Movement?

Here are a few reasons why smooth mouse movement is important in games:

  • Improved User Experience: Smooth controls help the player feel more in control of their actions, which is essential for immersion, especially in first-person or third-person games.
  • Enhanced Precision: Fine-tuning mouse movement allows for more precise camera controls, which is crucial in shooter games or any games that involve careful aim.
  • Polished Look and Feel: It makes the game feel more professional and polished, which is essential in retaining players and keeping them engaged.
  • Reduces Motion Sickness: Jittery or overly sensitive camera movement can cause discomfort or motion sickness for players. Smooth mouse movement can help reduce this risk.

Setting Up Smooth Mouse Movement in Unity

Let's go through the steps to create smooth mouse movement in Unity.

Step 1: Create a New Script

First, create a new C# script that will control the mouse movement. You can name this script something like MouseLook.

using UnityEngine;

public class MouseLook : MonoBehaviour
{
    public float mouseSensitivity = 100f;
    public Transform playerBody;

    private float xRotation = 0f;

    void Start()
    {
        // Lock the cursor in the middle of the screen
        Cursor.lockState = CursorLockMode.Locked;
    }

    void Update()
    {
        // Get mouse movement input
        float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
        float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;

        // Invert the Y-axis for a more natural control feel
        xRotation -= mouseY;
        xRotation = Mathf.Clamp(xRotation, -90f, 90f);

        // Rotate the camera around the X-axis (up and down)
        transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);

        // Rotate the player object around the Y-axis (left and right)
        playerBody.Rotate(Vector3.up * mouseX);
    }
}

In this code:

  • mouseSensitivity controls how sensitive the mouse input is.
  • playerBody represents the player's transform, which rotates along the Y-axis for horizontal mouse movement.
  • The xRotation variable stores the current vertical rotation (up and down), and it is clamped between -90 and 90 degrees to prevent over-rotation.
  • We lock the mouse cursor to the center of the screen to avoid the cursor moving out of the game window.

Step 2: Attach the Script to the Camera

Now that the script is ready, go to your Unity scene and attach the MouseLook script to your camera (e.g., the Main Camera object).

Then, assign the playerBody field by dragging the player object (usually the character controller or an empty game object representing the player) into the script's Player Body field in the Inspector.

Step 3: Tweak Mouse Sensitivity

You can experiment with the mouseSensitivity value to achieve the level of control you want. A good starting point is 100, but you can adjust it higher or lower depending on your desired level of precision.

Step 4: Handling Input Smoothness

For even smoother movement, you can apply interpolation to the mouse input values. This ensures that the camera transitions smoothly between each frame, rather than jumping from one position to the next. Here's an example of how to implement that:

using UnityEngine;

public class MouseLook : MonoBehaviour
{
    public float mouseSensitivity = 100f;
    public Transform playerBody;

    private float xRotation = 0f;
    private Vector2 smoothInputVelocity;
    public float smoothTime = 0.1f;
    private Vector2 currentMouseDelta;

    void Start()
    {
        Cursor.lockState = CursorLockMode.Locked;
    }

    void Update()
    {
        // Get raw mouse input
        Vector2 targetMouseDelta = new Vector2(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y")) * mouseSensitivity;

        // Smooth the mouse input
        currentMouseDelta = Vector2.SmoothDamp(currentMouseDelta, targetMouseDelta, ref smoothInputVelocity, smoothTime);

        xRotation -= currentMouseDelta.y * Time.deltaTime;
        xRotation = Mathf.Clamp(xRotation, -90f, 90f);

        transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
        playerBody.Rotate(Vector3.up * currentMouseDelta.x * Time.deltaTime);
    }
}

This updated version introduces smoothing using Vector2.SmoothDamp. The smoothTime variable controls how smooth the transition should be. Lower values make the movement more responsive, while higher values make it slower and more gradual.

Step 5: Testing and Fine-Tuning

Once you have the script in place, test the game and adjust the sensitivity and smoothing values based on how smooth you want the mouse movement to be. You can also adjust the clamping angle to allow more or less freedom of camera movement.

Conclusion

By implementing smooth mouse movement in your Unity project, you can significantly enhance the player experience by offering precise and fluid camera control. This tutorial walked you through setting up a basic mouse movement system and enhancing it with smoothing techniques.

Links
Unity 6