using UnityEngine;
using Unity.Cinemachine; // Cinemachine 3.x (Unity 6). For Cinemachine 2.x see the note at the bottom.
/// <summary>
/// GAME 1303 Tier 1 — Makes the Cinemachine camera follow the player automatically,
/// even if the player is spawned from a prefab or the scene was just loaded.
///
/// SETUP (Cinemachine 3.x, Unity 6)
/// 1. Window -> Package Manager -> install "Cinemachine".
/// 2. GameObject -> Cinemachine -> 2D Camera. This adds a CinemachineBrain to Main Camera
/// and creates a "CinemachineCamera" object with a Position Composer.
/// 3. Add this script to that CinemachineCamera object. Leave Target empty — it
/// finds the "Player" tag on Start.
/// 4. Recommended Position Composer values for platformers:
/// Damping X 0.5 Damping Y 1.0 (camera lags a little, feels smooth)
/// Dead Zone Width 0.1 Height 0.2 (small moves don't jiggle the camera)
/// Screen Y 0.6 (player sits a bit below center so you see more sky)
/// 5. Level bounds: add a CinemachineConfiner2D component to the same object, then
/// create a GameObject with a PolygonCollider2D (Is Trigger) outlining the level
/// and drag it into the confiner's Bounding Shape 2D slot.
///
/// For a static camera (Weenie Maze, MythoClash arena, Cozy Critters) you don't need
/// this script at all — just don't give the camera a Follow target.
/// </summary>
[RequireComponent(typeof(CinemachineCamera))]
public class CameraTargetAssigner : MonoBehaviour
{
[Tooltip("Leave empty to find the object tagged 'Player'.")]
public Transform target;
void Start()
{
if (target == null)
{
GameObject p = GameObject.FindGameObjectWithTag("Player");
if (p != null) target = p.transform;
}
Assign();
}
/// <summary>Call this if the player object is replaced (e.g. CharacterSwapper).</summary>
public void SetTarget(Transform newTarget)
{
target = newTarget;
Assign();
}
void Assign()
{
if (target == null) { Debug.LogWarning("CameraTargetAssigner: no target found."); return; }
var cam = GetComponent<CinemachineCamera>();
cam.Follow = target;
cam.LookAt = target;
}
}
/* ---- Cinemachine 2.x (Unity 2022 or earlier) version ----
Replace the using line with: using Cinemachine;
Replace CinemachineCamera with: CinemachineVirtualCamera
Everything else is identical (Follow / LookAt exist on both).
*/