Unity CrossHair Follow Mouse & Unity Change Camera View Tutorial - 3D Game Development Course 2022

3 min read 2 hours ago
Published on Oct 26, 2024 This response is partially generated with the help of AI. It may contain inaccuracies.

Table of Contents

Introduction

In this tutorial, we will learn how to implement a crosshair that follows the mouse and how to change the camera view in Unity. This guide is part of a 3D game development course aimed at helping you become a creative game developer using the Unity game engine. By the end of this tutorial, you'll be equipped with essential skills to enhance your game's user interface and camera mechanics.

Step 1: Setting Up the Crosshair

To make a crosshair that follows the mouse, follow these steps:

  1. Create a Crosshair UI Element

    • Open your Unity project.
    • Navigate to the Hierarchy window.
    • Right-click and select UI > Image.
    • Rename the new Image to "Crosshair".
  2. Adjust Crosshair Image Properties

    • Select the Crosshair in the Hierarchy.
    • In the Inspector panel, set the Image Source to your crosshair graphic.
    • Adjust the Rect Transform properties:
      • Set Anchor to the center of the screen.
      • Set Pivot to (0.5, 0.5).
      • Adjust Width and Height to match your graphic size.
  3. Create a Script to Follow Mouse

    • Right-click in the Project window, select Create > C# Script, and name it "CrosshairFollowMouse".
    • Open the script and implement the following code:
    using UnityEngine;
    
    public class CrosshairFollowMouse : MonoBehaviour
    {
        void Update()
        {
            Vector3 mousePosition = Input.mousePosition;
            mousePosition.z = 10; // Set a distance from the camera
            transform.position = Camera.main.ScreenToWorldPoint(mousePosition);
        }
    }
    
  4. Attach the Script

    • Drag the "CrosshairFollowMouse" script onto the Crosshair object in the Hierarchy.

Step 2: Changing the Camera View

Now, let's set up the camera to switch views based on user input.

  1. Create a Camera Control Script

    • In the Project window, right-click and select Create > C# Script, and name it "CameraControl".
    • Open the script and add the following code:
    using UnityEngine;
    
    public class CameraControl : MonoBehaviour
    {
        public Camera mainCamera;
        public Camera alternateCamera;
    
        void Update()
        {
            if (Input.GetKeyDown(KeyCode.C))
            {
                SwitchCamera();
            }
        }
    
        void SwitchCamera()
        {
            mainCamera.gameObject.SetActive(!mainCamera.gameObject.activeSelf);
            alternateCamera.gameObject.SetActive(!alternateCamera.gameObject.activeSelf);
        }
    }
    
  2. Assign Cameras in the Inspector

    • Create two cameras in your scene if you haven't already.
    • Select your main camera and alternate camera and configure them as needed.
    • Attach the "CameraControl" script to an empty GameObject in your scene.
    • In the Inspector, link the mainCamera and alternateCamera fields to their respective camera objects.
  3. Test the Camera Switch

    • Enter Play mode in Unity.
    • Press the C key to toggle between the two camera views.

Conclusion

In this tutorial, we covered how to create a crosshair that follows the mouse cursor and how to implement a camera switch in Unity. These skills are fundamental for improving user experience in your 3D games. As a next step, consider experimenting with different crosshair designs and camera angles to enhance your game's visual appeal and functionality. Happy game developing!