Merge branch 'master' of github.com:WillJeynes/Brackeys2026-2
This commit is contained in:
@@ -1,47 +1,140 @@
|
||||
using UnityEngine;
|
||||
using Unity.Cinemachine;
|
||||
using UnityEngine.InputSystem;
|
||||
using UnityEngine.UI;
|
||||
using UnityEngine.EventSystems;
|
||||
|
||||
public class CameraController : MonoBehaviour
|
||||
{
|
||||
[Header("Cinemachine 3 Setup")]
|
||||
[Tooltip("The single, reusable CinemachineCamera meant for panning to targets.")]
|
||||
public CinemachineCamera focusCamera;
|
||||
public CinemachineCamera focusCameraA;
|
||||
public CinemachineCamera focusCameraB;
|
||||
public CinemachineCamera explorationCamera;
|
||||
|
||||
public int activePriority = 20;
|
||||
public int inactivePriority = 10;
|
||||
|
||||
[Header("Raycast Setup")]
|
||||
public Camera mainUnityCamera;
|
||||
[Tooltip("Put your characters/doors on a specific Layer to ensure we only click them.")]
|
||||
public LayerMask interactableLayer;
|
||||
|
||||
[Header("UI Setup")]
|
||||
public Button talkButton;
|
||||
public Button doorButton; // New button for the doors
|
||||
private GuardManager currentGuardManager;
|
||||
private DoorManager currentDoorManager; // New reference for the door
|
||||
|
||||
[Header("Hover Highlight Settings")]
|
||||
[Tooltip("The colour the object will turn when hovered.")]
|
||||
public Color highlightColour = Color.yellow;
|
||||
|
||||
// Using 'Renderer' allows this to work on both 3D Meshes and 2D Sprites
|
||||
private Renderer currentlyHoveredRenderer;
|
||||
private Color originalColour;
|
||||
|
||||
[Header("Exploration Nudge Settings")]
|
||||
public float nudgeSpeed = 15f;
|
||||
public float maxNudgeAngle = 8f;
|
||||
public float edgeThickness = 50f;
|
||||
|
||||
[Header("Focus Nudge Settings")]
|
||||
public float focusNudgeSpeed = 5f;
|
||||
public float maxFocusOffset = 2f;
|
||||
|
||||
// Exploration rotation tracker (Horizontal only now)
|
||||
private Quaternion startingRotation;
|
||||
private float currentYaw = 0f;
|
||||
|
||||
// Ping-Pong Focus Trackers
|
||||
private bool useCameraA = true;
|
||||
private Transform dynamicFocusTargetA;
|
||||
private Transform dynamicFocusTargetB;
|
||||
private Transform activeDynamicTarget;
|
||||
private Transform currentInteractableTransform;
|
||||
|
||||
// Focus offset (Horizontal only now)
|
||||
private float currentFocusOffsetX = 0f;
|
||||
|
||||
void Start()
|
||||
{
|
||||
if (explorationCamera != null)
|
||||
{
|
||||
startingRotation = explorationCamera.transform.rotation;
|
||||
}
|
||||
|
||||
dynamicFocusTargetA = new GameObject("DynamicFocusTarget_A").transform;
|
||||
dynamicFocusTargetB = new GameObject("DynamicFocusTarget_B").transform;
|
||||
|
||||
if (talkButton != null)
|
||||
{
|
||||
talkButton.gameObject.SetActive(false);
|
||||
talkButton.onClick.AddListener(OnTalkButtonClicked);
|
||||
}
|
||||
|
||||
if (doorButton != null)
|
||||
{
|
||||
doorButton.gameObject.SetActive(false);
|
||||
doorButton.onClick.AddListener(OnDoorButtonClicked);
|
||||
}
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
if (Mouse.current == null) return;
|
||||
|
||||
// Check for hovering every frame
|
||||
HandleHover();
|
||||
HandleEdgeNudge();
|
||||
|
||||
// On Left Mouse Click: Cast the invisible laser
|
||||
if (Mouse.current.leftButton.wasPressedThisFrame)
|
||||
{
|
||||
if (EventSystem.current != null && EventSystem.current.IsPointerOverGameObject())
|
||||
{
|
||||
// TRIPWIRE 1: ADD THIS LINE HERE
|
||||
Debug.Log("⚠️ Click cancelled! The EventSystem thinks your mouse is hovering over a UI element.");
|
||||
return;
|
||||
}
|
||||
HandleInteractionClick();
|
||||
}
|
||||
|
||||
// On Right Mouse Click: Return to exploration
|
||||
if (Mouse.current.rightButton.wasPressedThisFrame)
|
||||
{
|
||||
ReturnToMainCamera();
|
||||
}
|
||||
|
||||
if (Keyboard.current != null && Keyboard.current.escapeKey.wasPressedThisFrame)
|
||||
{
|
||||
ResetCameraPositions();
|
||||
}
|
||||
}
|
||||
|
||||
private void HandleEdgeNudge()
|
||||
{
|
||||
Vector2 mousePos = Mouse.current.position.ReadValue();
|
||||
float yawInput = 0f;
|
||||
|
||||
// Check horizontal edges only
|
||||
if (mousePos.x < edgeThickness) yawInput = -1f;
|
||||
else if (mousePos.x > Screen.width - edgeThickness) yawInput = 1f;
|
||||
|
||||
bool isFocused = focusCameraA.Priority >= activePriority || focusCameraB.Priority >= activePriority;
|
||||
|
||||
if (yawInput != 0f)
|
||||
{
|
||||
if (isFocused && currentInteractableTransform != null && activeDynamicTarget != null)
|
||||
{
|
||||
currentFocusOffsetX += yawInput * focusNudgeSpeed * Time.deltaTime;
|
||||
currentFocusOffsetX = Mathf.Clamp(currentFocusOffsetX, -maxFocusOffset, maxFocusOffset);
|
||||
|
||||
// Move the invisible point horizontally relative to the camera
|
||||
activeDynamicTarget.position = currentInteractableTransform.position
|
||||
+ (mainUnityCamera.transform.right * currentFocusOffsetX);
|
||||
}
|
||||
else if (!isFocused && explorationCamera != null)
|
||||
{
|
||||
currentYaw += yawInput * nudgeSpeed * Time.deltaTime;
|
||||
currentYaw = Mathf.Clamp(currentYaw, -maxNudgeAngle, maxNudgeAngle);
|
||||
|
||||
Quaternion yawRotation = Quaternion.AngleAxis(currentYaw, Vector3.up);
|
||||
explorationCamera.transform.rotation = startingRotation * yawRotation;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void HandleHover()
|
||||
@@ -51,16 +144,13 @@ public class CameraController : MonoBehaviour
|
||||
|
||||
if (Physics.Raycast(ray, out RaycastHit hit, 100f, interactableLayer))
|
||||
{
|
||||
// Look for any type of renderer (Mesh or Sprite)
|
||||
Renderer hitRenderer = hit.transform.GetComponentInChildren<Renderer>();
|
||||
|
||||
if (hitRenderer != null && hitRenderer != currentlyHoveredRenderer)
|
||||
{
|
||||
RemoveHighlight();
|
||||
|
||||
currentlyHoveredRenderer = hitRenderer;
|
||||
|
||||
// Safely apply the colour tint based on whether it is 2D or 3D
|
||||
if (currentlyHoveredRenderer is SpriteRenderer sprite)
|
||||
{
|
||||
originalColour = sprite.color;
|
||||
@@ -83,7 +173,6 @@ public class CameraController : MonoBehaviour
|
||||
{
|
||||
if (currentlyHoveredRenderer != null)
|
||||
{
|
||||
// Revert the colour safely
|
||||
if (currentlyHoveredRenderer is SpriteRenderer sprite)
|
||||
{
|
||||
sprite.color = originalColour;
|
||||
@@ -98,31 +187,111 @@ public class CameraController : MonoBehaviour
|
||||
|
||||
private void HandleInteractionClick()
|
||||
{
|
||||
// 1. Get the mouse position using the new Input System
|
||||
Vector2 mousePos = Mouse.current.position.ReadValue();
|
||||
Ray ray = mainUnityCamera.ScreenPointToRay(mousePos);
|
||||
|
||||
// 2. Shoot the raycast
|
||||
if (Physics.Raycast(ray, out RaycastHit hit, 100f, interactableLayer))
|
||||
{
|
||||
Transform clickedTarget = hit.transform;
|
||||
// TRIPWIRE 2: ADD THIS LINE HERE
|
||||
Debug.Log($"🎯 Raycast successfully hit: {hit.transform.name}");
|
||||
|
||||
currentInteractableTransform = hit.transform;
|
||||
currentFocusOffsetX = 0f;
|
||||
|
||||
focusCamera.Follow = clickedTarget;
|
||||
focusCamera.LookAt = clickedTarget;
|
||||
focusCamera.Priority = activePriority;
|
||||
CinemachineCamera nextCam = useCameraA ? focusCameraA : focusCameraB;
|
||||
CinemachineCamera oldCam = useCameraA ? focusCameraB : focusCameraA;
|
||||
Transform nextTarget = useCameraA ? dynamicFocusTargetA : dynamicFocusTargetB;
|
||||
|
||||
Debug.Log($"Successfully panning to: {clickedTarget.name}");
|
||||
// TRIPWIRE 3: ADD THIS SAFETY CHECK HERE
|
||||
if (nextCam == null)
|
||||
{
|
||||
Debug.LogError("🚨 ERROR: One of your Focus Cameras is missing in the GameManager Inspector!");
|
||||
return;
|
||||
}
|
||||
|
||||
nextTarget.position = currentInteractableTransform.position;
|
||||
nextCam.Follow = currentInteractableTransform;
|
||||
nextCam.LookAt = nextTarget;
|
||||
|
||||
nextCam.Priority = activePriority;
|
||||
oldCam.Priority = inactivePriority;
|
||||
|
||||
activeDynamicTarget = nextTarget;
|
||||
useCameraA = !useCameraA;
|
||||
|
||||
// Logic to determine which UI button to show
|
||||
GuardManager guard = hit.transform.GetComponent<GuardManager>();
|
||||
DoorManager door = hit.transform.GetComponent<DoorManager>();
|
||||
|
||||
// Hide both first to be safe, then reveal the correct one
|
||||
if (talkButton != null) talkButton.gameObject.SetActive(false);
|
||||
if (doorButton != null) doorButton.gameObject.SetActive(false);
|
||||
|
||||
if (guard != null)
|
||||
{
|
||||
currentGuardManager = guard;
|
||||
if (talkButton != null) talkButton.gameObject.SetActive(true);
|
||||
}
|
||||
else if (door != null)
|
||||
{
|
||||
currentDoorManager = door;
|
||||
if (doorButton != null) doorButton.gameObject.SetActive(true);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// TRIPWIRE 4: ADD THIS LINE HERE
|
||||
Debug.Log("💨 Raycast hit nothing on the Interactable layer. Returning to main camera.");
|
||||
ReturnToMainCamera();
|
||||
}
|
||||
}
|
||||
|
||||
private void OnTalkButtonClicked()
|
||||
{
|
||||
if (currentGuardManager != null)
|
||||
{
|
||||
DialogManager.Instance.StartDialog(currentGuardManager);
|
||||
talkButton.gameObject.SetActive(false);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnDoorButtonClicked()
|
||||
{
|
||||
if (currentDoorManager != null)
|
||||
{
|
||||
// Add your door sequence logic here!
|
||||
Debug.Log("Door sequence started!");
|
||||
doorButton.gameObject.SetActive(false);
|
||||
}
|
||||
}
|
||||
|
||||
private void ReturnToMainCamera()
|
||||
{
|
||||
// Dropping the priority below your main exploration camera's priority
|
||||
// will cause the CinemachineBrain to automatically pan back to the player.
|
||||
focusCamera.Priority = inactivePriority;
|
||||
focusCameraA.Priority = inactivePriority;
|
||||
focusCameraB.Priority = inactivePriority;
|
||||
|
||||
// Optional housekeeping: clear the targets whilst dormant
|
||||
focusCamera.Follow = null;
|
||||
focusCamera.LookAt = null;
|
||||
focusCameraA.Follow = null; focusCameraA.LookAt = null;
|
||||
focusCameraB.Follow = null; focusCameraB.LookAt = null;
|
||||
|
||||
currentInteractableTransform = null;
|
||||
activeDynamicTarget = null;
|
||||
currentGuardManager = null;
|
||||
currentDoorManager = null;
|
||||
|
||||
if (talkButton != null) talkButton.gameObject.SetActive(false);
|
||||
if (doorButton != null) doorButton.gameObject.SetActive(false);
|
||||
}
|
||||
|
||||
private void ResetCameraPositions()
|
||||
{
|
||||
ReturnToMainCamera();
|
||||
|
||||
currentFocusOffsetX = 0f;
|
||||
currentYaw = 0f;
|
||||
|
||||
if (explorationCamera != null)
|
||||
{
|
||||
explorationCamera.transform.rotation = startingRotation;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user