Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Clock hands

Discussion in 'Scripting' started by Logerman, Dec 2, 2023.

  1. Logerman

    Logerman

    Joined:
    Nov 17, 2021
    Posts:
    1
    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);
    }
    }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,320
    Attached is a rotate via z-dragging script to get you started on the rotation.

    Structure things so you can drag any particular arm and it becomes the controlling source of data, then scale the other arm to do the right proportional thing.



    ALSO: If you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/

    - Do not TALK about code without posting it.
    - Do NOT post unformatted code.
    - Do NOT retype code. Use copy/paste properly using code tags.
    - Do NOT post screenshots of code.
    - Do NOT post photographs of code.
    - Do NOT attach entire scripts to your post.
    - ONLY post the relevant code, and then refer to it in your discussion.



    How to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220

    This is the bare minimum of information to report:

    - what you want
    - what you tried
    - what you expected to happen
    - what actually happened, log output, variable values, and especially any errors you see
    - links to actual Unity3D documentation you used to cross-check your work (CRITICAL!!!)

    The purpose of YOU providing links is to make our job easier, while simultaneously showing us that you actually put effort into the process. If you haven't put effort into finding the documentation, why should we bother putting effort into replying?
     

    Attached Files:

    Logerman likes this.