Home > Software design >  Unity - Reverse transform euler angles, so it counts clockwise?
Unity - Reverse transform euler angles, so it counts clockwise?

Time:06-18

I'm making some sort of a egg timer thing... where you can input your minutes by rotating the clock. transform.Eulerangles however goes the wrong way around:

enter image description here

But I need it to go this way around:

enter image description here

So I can can easily get my minutes by dividing the numbers by 6... but I can somehow not figure it out, how to flip it.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.Linq;

public class Test3 : MonoBehaviour
{
    [SerializeField] RectTransform rectTransform;
    int[] snapValues = new int[13]{0,30,60,90,120,150,180,210,240,270,300,330,360};
    public void Drag()
    {
        var dir = Input.mousePosition - Camera.main.WorldToScreenPoint(rectTransform.anchoredPosition);
        var angle = Mathf.Atan2(dir.x, dir.y) * Mathf.Rad2Deg;
        transform.rotation = Quaternion.AngleAxis(angle, -Vector3.forward);

        var nearest = snapValues.OrderBy(x => Mathf.Abs(x - transform.eulerAngles.z)).First();

        print(transform.eulerAngles.z);

    }
}

CodePudding user response:

To flip the result (i.e. 360 = 0, 180 = 180, 0 = 360), you can just subtract it from 360:

var angle = 360 - ( Mathf.Atan2(dir.x, dir.y) * Mathf.Rad2Deg );
  • Related