I’m trying to set up the clock in such a way that I can drag the minute and hour hands using LMB. And when they get into the right position (indicate the right time), my event will work. But I absolutely can’t figure out how to drag the arrows
using UnityEngine;
public class ClockController : MonoBehaviour
{
public Transform hourHand;
public Transform minuteHand;
private const float degreesPerHour = 6f;
private const float degreesPerMinute = 6f;
private Vector3 clockCenter;
private bool isDragging = false;
void Start()
{
clockCenter = transform.position;
SetClockHandsRotation(30, 0);
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
isDragging = true;
}
if (isDragging)
{
Vector3 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
mousePosition.z = 0;
Vector3 direction = mousePosition - clockCenter;
float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
float roundedAngle = Mathf.Round(angle / 30f) * 30f;
hourHand.rotation = Quaternion.Euler(0, 0, +roundedAngle);
minuteHand.rotation = Quaternion.Euler(0, 0, +roundedAngle);
}
if (Input.GetMouseButtonUp(0))
{
isDragging = false;
}
}
void SetClockHandsRotation(float hours, float minutes)
{
float hourRotation = hours * degreesPerHour;
float minuteRotation = minutes * degreesPerMinute;
hourHand.rotation = Quaternion.Euler(0, 0, +hourRotation);
minuteHand.rotation = Quaternion.Euler(0, 0, -minuteRotation);
}
}