1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

バーチャルパッドアセット『Joystick Pack』で初回タッチ時のみ描画がズレてしまう

Posted at

バーチャルパッドを簡単に導入できるアセット、『Joystick Pack』

Fixed Joystick、Floating Joystick、Dynamic Joystick の3つのタイプのバーチャルパッドを導入できます。 が、Floating JoystickCanvasのRenderModeがScreenSpaceCamera の時に使用すると、初回のタッチ時にパッドの描画位置がズレます。(Joystick Pack Version2.1)

パッドの描画位置算出時に使用する、カメラの参照がnullであるために起こるようなので、OnDrag内でカメラオブジェクトを取得している部分を、Start内に移動させることで対応できました。

Joystick.cs
    protected virtual void Start()
    {
        HandleRange = handleRange;
        DeadZone = deadZone;
        baseRect = GetComponent<RectTransform>();
        canvas = GetComponentInParent<Canvas>();
        if (canvas == null)
            Debug.LogError("The Joystick is not placed inside a canvas");

        Vector2 center = new Vector2(0.5f, 0.5f);
        background.pivot = center;
        handle.anchorMin = center;
        handle.anchorMax = center;
        handle.pivot = center;
        handle.anchoredPosition = Vector2.zero;

+        cam = null;
+        if (canvas.renderMode == RenderMode.ScreenSpaceCamera)
+            cam = canvas.worldCamera;
    }

    public void OnDrag(PointerEventData eventData)
    {
-        cam = null;
-        if (canvas.renderMode == RenderMode.ScreenSpaceCamera)
-            cam = canvas.worldCamera;

        Vector2 position = RectTransformUtility.WorldToScreenPoint(cam, background.position);
        Vector2 radius = background.sizeDelta / 2;
        input = (eventData.position - position) / (radius * canvas.scaleFactor);
        FormatInput();
        HandleInput(input.magnitude, input.normalized, radius, cam);
        handle.anchoredPosition = input * radius * handleRange;
    }
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?