1
\$\begingroup\$

First of all, here's the video of what my problem really is. (Sorry for bad quality)

As you can see, the red circle is the center of the visual joystick. The green one indicates my finger and the white circle is the handle of my joystick.

As you can see it works but the handle goes faster than my finger, I don't know why this happens

Here's the code for setting the handle's position:

            RawDirection = Input.GetTouch(CurrentFingerID).position -
                (Vector2)BGRect.position;

            if (RawDirection.magnitude > BGRect.sizeDelta.x / 2)
                RawDirection = RawDirection.normalized * BGRect.sizeDelta.x / 2;

            HandleRect.anchoredPosition = RawDirection;

meaning of the variables:

  • CurrentFingerID is used to save which finger is on the joystick currently.
  • BGRect is the RectTransform attached to the BG (see the pic below)
  • HandleRect is the RectTransform attached to the Handle (see the pic below)

And these are BG and Handle:

BG and Handle

BG and Handle settings:

BG

Handle

I think RawDirection should be divided by some number to fix this issue. Any help is appreciated and feel free to ask for additional info

\$\endgroup\$
4
  • \$\begingroup\$ Is the BGRect scaled? \$\endgroup\$
    – Mangata
    Commented Jun 22, 2022 at 9:27
  • \$\begingroup\$ @Mangata I doubted that the problem is scaling too but it's set to 1. I'll add their images in my questions \$\endgroup\$
    – Arian_ki
    Commented Jun 22, 2022 at 10:27
  • 1
    \$\begingroup\$ touch.Position is measured in screen pixels. RectTransform.anchoredPosition is measured in the units of its containing rect. Did you mean to convert between these coordinate systems explicitly, using RectTransformUtility.ScreenPointToLocalPointInRectangle? \$\endgroup\$
    – DMGregory
    Commented Jun 22, 2022 at 12:25
  • \$\begingroup\$ Oh, yes. I've tried that before but it didn't work. Now I tested it again, it works (with some minor changes). I'll post it as an answer \$\endgroup\$
    – Arian_ki
    Commented Jun 22, 2022 at 12:59

1 Answer 1

3
\$\begingroup\$

It looks like I should have used RectTransformUtility:

RectTransformUtility.ScreenPointToLocalPointInRectangle(ResponseArea, Input.touches[CurrentFingerID].position, null, out FingerPositionInRect);
RawDirection = FingerPositionInRect - ((BGRect.anchorMax + ResponseArea.sizeDelta) - BGRect.anchoredPosition;

(ResponseArea is the RectTransform of the Joystick)

\$\endgroup\$

You must log in to answer this question.

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