Files
JuegoSim/Assets/_Project/Scripts/Code/Camera/ViewCubeController.cs
T
2026-07-21 08:56:10 +03:00

132 lines
4.6 KiB
C#

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class ViewCubeController : MonoBehaviour
{
[Header("References")]
public Camera mainCam; // The Base Camera
public Camera viewCubeCam; // The Overlay Camera/Texture Camera
private CameraControl CameraController; // Your camera movement script
public RawImage viewCubeImage;
private EventTrigger eventTrigger;
private bool isPointerInViewCube = false;
[Header("Settings")]
private Transform cubeContainer;
public LayerMask viewCubeLayer;
// NEW: We need the RectTransform to calculate positions correctly
private RectTransform rawImageRect;
// NEW: Store the last ray for debugging in Gizmos
private Ray debugRay;
private void Start()
{
cubeContainer = transform;
if (mainCam) CameraController = mainCam.GetComponent<CameraControl>();
// Cache the RectTransform so we can access width/height later
rawImageRect = viewCubeImage.GetComponent<RectTransform>();
eventTrigger = viewCubeImage.GetComponent<EventTrigger>();
EventTrigger.Entry entry = new EventTrigger.Entry();
entry.eventID = EventTriggerType.PointerEnter;
entry.callback.AddListener((data) => { isPointerInViewCube = true; });
eventTrigger.triggers.Add(entry);
entry = new EventTrigger.Entry();
entry.eventID = EventTriggerType.PointerExit;
entry.callback.AddListener((data) => { isPointerInViewCube = false; });
eventTrigger.triggers.Add(entry);
}
private void Update()
{
RotateCube();
if (CameraController != null)
{
CameraController.isInputLocked = isPointerInViewCube;
}
if (isPointerInViewCube)
{
CalculateCurrentRay();
}
DetectClick();
}
void RotateCube()
{
if (mainCam == null || cubeContainer == null) return;
cubeContainer.rotation = mainCam.transform.rotation;
}
// NEW FUNCTION: Handles the math to convert Screen Pixels -> UI Pixels -> Camera Ray
void CalculateCurrentRay()
{
Vector2 localPoint;
// 1. Convert Screen Mouse Point to a point inside the RawImage rectangle
// The 'null' param works for "Screen Space - Overlay" canvas.
// If your canvas is "Screen Space - Camera", pass the UI Camera instead of null.
if (RectTransformUtility.ScreenPointToLocalPointInRectangle(
rawImageRect,
Input.mousePosition,
null,
out localPoint))
{
// 2. Normalize positions to 0-1 range (Viewport coordinates)
// LocalPoint (0,0) is the center of the image. We shift it by +0.5 to make (0,0) the bottom-left.
float normalizedX = (localPoint.x / rawImageRect.rect.width) + 0.5f;
float normalizedY = (localPoint.y / rawImageRect.rect.height) + 0.5f;
// 3. Create the ray from the ViewCube Camera using these coordinates
debugRay = viewCubeCam.ViewportPointToRay(new Vector3(normalizedX, normalizedY, 0));
}
}
void DetectClick()
{
if (Input.GetMouseButtonDown(0) && isPointerInViewCube)
{
// We use the ray calculated in CalculateCurrentRay()
if (Physics.Raycast(debugRay, out RaycastHit hit, 100f, viewCubeLayer))
{
Debug.Log("View Cube Hit: " + hit.collider.name);
Vector3 targetDir = Vector3.zero;
switch (hit.collider.name)
{
case "Front": targetDir = Vector3.forward; break;
case "Back": targetDir = Vector3.back; break;
case "Left": targetDir = Vector3.left; break;
case "Right": targetDir = Vector3.right; break;
case "Top": targetDir = Vector3.up; break;
case "Bottom": targetDir = Vector3.down; break;
}
if (targetDir != Vector3.zero && CameraController != null)
{
CameraController.SnapToDirection(targetDir);
}
}
}
}
// DRAW GIZMOS: This will now draw the ray based on where your mouse is hovering
private void OnDrawGizmos()
{
if (viewCubeCam == null) return;
Gizmos.color = Color.red;
// Draw the ray stored in debugRay
Gizmos.DrawLine(debugRay.origin, debugRay.origin + debugRay.direction * 100f);
// Draw a small sphere at the ray start point to confirm it's coming from the camera
Gizmos.DrawSphere(debugRay.origin, 0.2f);
}
}