1
\$\begingroup\$

I want to create the above object that will rotate on z-axis following the direction of a finger. Like a dial. I have written the following code and attached it to that element:

private float rotationRate = 3.0f;

    void Update()
    {
        // get the user touch input
        foreach (Touch touch in Input.touches)
        {
            Debug.Log("Touching at: " + touch.position);

            if (touch.phase == TouchPhase.Began)
            {
                Debug.Log("Touch phase began at: " + touch.position);
            }
            else if (touch.phase == TouchPhase.Moved)
            {
                Debug.Log("Touch phase Moved");
                transform.Rotate(touch.deltaPosition.y * rotationRate,
                                 -touch.deltaPosition.x * rotationRate, 0, Space.World);
            }
            else if (touch.phase == TouchPhase.Ended)
            {
                Debug.Log("Touch phase Ended");
            }
        }
    }

But the element does not move at all. Any ideas?

EDIT:

Here is a gif for better understanding:

enter image description here

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

I used this Script and it did rotate the sprite but not like the GIF you have posted so i mad a little bit of changes and it worked fine also the value of "rotationRate" was way to high so reduced it to "0.4f" but is totally up to you what do you like, Any way here is you updated script:

[SerializeField, Range(0, 3)] private float rotationRate = 0.4f;

    void Update()
    {
        foreach (Touch touch in Input.touches)
        {
            Debug.Log("Touching at: " + touch.position);

            if (touch.phase == TouchPhase.Began)
            {
                Debug.Log("Touch phase began at: " + touch.position);
            }
            else if (touch.phase == TouchPhase.Moved)
            {
                Debug.Log("Touch phase Moved");
                transform.Rotate(0, 0, -touch.deltaPosition.x * rotationRate, Space.World);
            }
            else if (touch.phase == TouchPhase.Ended)
            {
                Debug.Log("Touch phase Ended");
            }
        }
    }
\$\endgroup\$
5
  • \$\begingroup\$ I tried the code again using another device and yes it works. I don't know maybe the issue was with the device. \$\endgroup\$ Sep 5, 2019 at 13:53
  • \$\begingroup\$ Ok. I am glad it worked. If my answar helped you out then accept and up vote my answar. \$\endgroup\$
    – Swati
    Sep 5, 2019 at 15:58
  • \$\begingroup\$ another small question, it will not be worth asking as a new question so I am asking here. If I want to limit the movement of z-angle between -45 and 45 how can I do it? I tried limiting transform.eulerAngles.z between them but it causes abnormal behavior. \$\endgroup\$ Sep 6, 2019 at 9:30
  • \$\begingroup\$ Have you tried Mathf.Clamp? here is documentation about it: docs.unity3d.com/ScriptReference/Mathf.Clamp.html \$\endgroup\$
    – Swati
    Sep 6, 2019 at 12:53
  • \$\begingroup\$ Math.Clamp resets the angle to -45 after increasing form 0. I Found another solution online, Needed a little modification but it worked. \$\endgroup\$ Sep 7, 2019 at 2:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .