游戏杆在Unity中意外移动

时间:2019-02-20 06:33:12

标签: unity3d joystick

上下文:我正在项目中Unity资产商店上使用Fenerax Studios的Joystick Pack中的FixedJoystick。

问题是,每当我触摸操纵杆(甚至不移动操纵杆)时,手柄都会立即移至右上角,结果模型会向下移动。

下面是说明操纵杆问题的图像

enter image description here

图像中的红叉表示用户触摸操纵杆的位置,手柄没有按预期移动到那里,而是向上跳。

代码如下:

using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class FixedJoystick : Joystick
{
    Vector2 joystickPosition = Vector2.zero;
    public GameObject heldDownBlockRegion;
    private Camera cam = new Camera();
    private bool inARMode = true;



void Start()
{
    joystickPosition = RectTransformUtility.WorldToScreenPoint(cam, background.position);
    gameObject.SetActive(false);

}


public override void OnDrag(PointerEventData eventData)
{
    Vector2 direction = eventData.position - joystickPosition;
    inputVector = (direction.magnitude > background.sizeDelta.x / 2f) ? direction.normalized : direction / (background.sizeDelta.x / 2f);
    ClampJoystick();
    handle.anchoredPosition = (inputVector * background.sizeDelta.x / 2f) * handleLimit;
}

public override void OnPointerDown(PointerEventData eventData)
{
    heldDownBlockRegion.SetActive(true);
    OnDrag(eventData);
}

public override void OnPointerUp(PointerEventData eventData)
{
    heldDownBlockRegion.SetActive(false);
    inputVector = Vector2.zero;
    handle.anchoredPosition = Vector2.zero;
}

public void changeToAR()
{
    showJoyStick(false);
    inARMode = true;
}

public void changeToNonAR()
{
    inARMode = false;
}

public void showJoyStick(bool action)
{
    if (!inARMode)
    {
        if (action == false)
        {
            heldDownBlockRegion.SetActive(false);
            inputVector = Vector2.zero;
            handle.anchoredPosition = Vector2.zero;
        }
        gameObject.SetActive(action);
    }
}
}

我对此很陌生,非常感谢您的帮助。谢谢!

0 个答案:

没有答案