128 lines
4.0 KiB
C#
128 lines
4.0 KiB
C#
using UnityEngine;
|
|
using Unity.Cinemachine;
|
|
using UnityEngine.InputSystem;
|
|
|
|
public class CameraController : MonoBehaviour
|
|
{
|
|
[Header("Cinemachine 3 Setup")]
|
|
[Tooltip("The single, reusable CinemachineCamera meant for panning to targets.")]
|
|
public CinemachineCamera focusCamera;
|
|
|
|
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("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;
|
|
|
|
void Update()
|
|
{
|
|
if (Mouse.current == null) return;
|
|
|
|
// Check for hovering every frame
|
|
HandleHover();
|
|
|
|
// On Left Mouse Click: Cast the invisible laser
|
|
if (Mouse.current.leftButton.wasPressedThisFrame)
|
|
{
|
|
HandleInteractionClick();
|
|
}
|
|
|
|
// On Right Mouse Click: Return to exploration
|
|
if (Mouse.current.rightButton.wasPressedThisFrame)
|
|
{
|
|
ReturnToMainCamera();
|
|
}
|
|
}
|
|
|
|
private void HandleHover()
|
|
{
|
|
Vector2 mousePos = Mouse.current.position.ReadValue();
|
|
Ray ray = mainUnityCamera.ScreenPointToRay(mousePos);
|
|
|
|
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;
|
|
sprite.color = highlightColour;
|
|
}
|
|
else
|
|
{
|
|
originalColour = currentlyHoveredRenderer.material.color;
|
|
currentlyHoveredRenderer.material.color = highlightColour;
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
RemoveHighlight();
|
|
}
|
|
}
|
|
|
|
private void RemoveHighlight()
|
|
{
|
|
if (currentlyHoveredRenderer != null)
|
|
{
|
|
// Revert the colour safely
|
|
if (currentlyHoveredRenderer is SpriteRenderer sprite)
|
|
{
|
|
sprite.color = originalColour;
|
|
}
|
|
else
|
|
{
|
|
currentlyHoveredRenderer.material.color = originalColour;
|
|
}
|
|
currentlyHoveredRenderer = null;
|
|
}
|
|
}
|
|
|
|
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;
|
|
|
|
focusCamera.Follow = clickedTarget;
|
|
focusCamera.LookAt = clickedTarget;
|
|
focusCamera.Priority = activePriority;
|
|
|
|
Debug.Log($"Successfully panning to: {clickedTarget.name}");
|
|
}
|
|
}
|
|
|
|
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;
|
|
|
|
// Optional housekeeping: clear the targets whilst dormant
|
|
focusCamera.Follow = null;
|
|
focusCamera.LookAt = null;
|
|
}
|
|
} |