2. Camera movement: player and mouse following

Open the project

Basic player camera

Scripted player camera

Scripted player camera: Basic follow

using UnityEngine;

public class CameraMovement : MonoBehaviour
{
    // Reference to the player GameObject to follow
    public Transform player;

    // Offset between the camera and the player
    private Vector3 offset;

    // Called once when the script is enabled
    void Start()
    {
        // Calculate the initial offset between the camera and the player
        offset = transform.position - player.position;
    }

    // Called every frame after all Update functions have been called
    void LateUpdate()
    {
        // Update the camera position to directly follow the player
        transform.position = player.position + offset;
    }
}

Scripted player camera: Smooth follow

using UnityEngine;

public class CameraMovement : MonoBehaviour
{
    // Reference to the player GameObject to follow
    public Transform player;

    // Speed at which the camera follows the player
    public float followSpeed = 4f;

    // Offset between the camera and the player
    private Vector3 offset;

    // Called once when the script is enabled
    void Start()
    {
        // Calculate the initial offset between the camera and the player
        offset = transform.position - player.position;
    }

    // Called every frame after all Update functions have been called
    void LateUpdate()
    {
        // Calculate the target position for the camera to move towards
        Vector3 targetPosition = player.position + offset;

        // Smoothly move the camera towards the target position using Lerp
        transform.position = Vector3.Lerp(transform.position, targetPosition, followSpeed * Time.deltaTime);
    }
}

Scripted mouse camera

Crosshair position

using UnityEngine;

public class CrosshairPlacement : MonoBehaviour
{
    void Update()
    {
        // Cast a ray from the camera through the mouse position
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;

        if (Physics.Raycast(ray, out hit))
        {
            // If the ray hits something, place the crosshair at the hit point
            transform.position = hit.point;
        }
    }
}
using UnityEngine;

public class CrosshairPlacement : MonoBehaviour
{
    // Layer mask for ground objects
    public LayerMask groundLayerMask;

    void Start()
    {
        //Set Cursor to not be visible
        //Cursor.visible = false;
    }

    void Update()
    {
        // Create a ray that starts from the main camera and passes through the mouse position on the screen
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        // RaycastHit provides details about where the ray hits an object
        RaycastHit hit;

        // Cast a ray into the scene and check for collisions
        if (Physics.Raycast(ray, out hit, Mathf.Infinity, groundLayerMask))
        {
            // If the ray hits something, place the crosshair at the hit point
            transform.position = new Vector3(hit.point.x, hit.point.y+1.3f, hit.point.z);
        }
    }
}

Follow in-between player and crosshair

using UnityEngine;

public class CameraMovement : MonoBehaviour
{
    // References to the Player and Crosshair
    public Transform player;
    public Transform crosshair;

    public float followSpeed = 4f;

    private Vector3 offset;

    void Start()
    {
        offset = transform.position - player.position;
    }

    void LateUpdate()
    {
        // Calculate the target position for the camera to move towards
        Vector3 targetPosition = this.PositionBetween(player.position, crosshair.position, 0.4f) + offset;

        transform.position = Vector3.Lerp(transform.position, targetPosition, followSpeed * Time.deltaTime);
    }

    // Calculate the position between two given positions (between player and crosshair)
    public Vector3 PositionBetween(Vector3 startPosition, Vector3 endPosition, float fraction)
    {
        return Vector3.Lerp(startPosition, endPosition, fraction);
    }
}

Cinemachine plugin

Minimap

Camera setup

UI Canvas

Final state: The floor is larger and I added a few 3D objects (a cube and a sphere).


< ^ >