RiftMayhem/Assets/Scripts/CharacterCreation/CameraTargetSelector.cs

59 lines
2.0 KiB
C#

using UnityEngine;
public class CameraTargetSelector : MonoBehaviour
{
[SerializeField] private OrbitCameraController orbitCamera; // Assign this in Inspector
[SerializeField] private LayerMask interactableLayer; // Filter for clickable objects
RaycastHit hit;
Ray ray;
CharacterSelectionController selectionController;
CharacterSelectionController possibleSelection;
private void Update()
{
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
// Check for collider hit in specific layer
if (Physics.Raycast(ray, out hit, 100f, interactableLayer))
{
possibleSelection = hit.transform.GetComponent<CharacterSelectionController>();
if (possibleSelection != null)
{
Debug.Log("hover" + hit.transform.name);
if (selectionController != possibleSelection)
{
if (selectionController != null)
selectionController.DisableHighlight();
selectionController = possibleSelection;
}
selectionController.Highlight();
}
else
{
if (selectionController != null)
selectionController.DisableHighlight();
}
}
if (Input.GetMouseButtonDown(0)) // Left-click
{
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
// Check for collider hit in specific layer
if (Physics.Raycast(ray, out hit, 100f, interactableLayer))
{
Debug.Log("hit" + hit.transform.name);
selectionController = hit.transform.GetComponent<CharacterSelectionController>();
if (selectionController == null)
return;
// Update orbit camera target to the clicked object
orbitCamera.SmoothSetTarget(hit.transform);
selectionController.ClickToggle();
}
}
}
}