Unity虚拟游戏杆不响应

Unity虚拟游戏杆不响应

本文介绍了Unity虚拟游戏杆不响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Unity中制作了一个虚拟游戏杆.不知何故,它没有反应.调试器永远不会输入事件OnPointerDownOnPointerUpOnDrag.

I made a virtual joystick in Unity. Somehow, it does not react. The events OnPointerDown , OnPointerUp and OnDrag never get entered by the debugger.

这是我的检查器设置的视图

This is a view of my inspector settings

这是我的代码,如您所见,我有一个用于某些数据的数据类,并在控制器类中调用它.

And this is my code, as you can see, I have a data class for some data and call it in the controller class.

public class VirtualJoystickData
{
    private Vector3 inputVector = Vector3.zero;
    public Vector3 InputVector { get { return inputVector; } set { inputVector = value; } } // the movementDirection

    private Image joystickBackgroundImage = GameObject.FindGameObjectWithTag("JoystickBackGroundImage").GetComponent<Image>(); // the joysticks background
    public Image JoystickBackgroundImage { get { return joystickBackgroundImage; } }

    private Image joystickImage = GameObject.FindGameObjectWithTag("Joystick").GetComponent<Image>(); // the joystick object to use
    public Image JoystickImage { get { return joystickImage; } }
}

public class VirtualJoystickController : Monobehaviour, IDragHandler, IPointerUpHandler, IPointerDownHandler
{
    private VirtualJoystickData data; // reference to the data class

    private void Start()
    {
        data = new VirtualJoystickData();
    }

    public virtual void OnPointerDown(PointerEventData e) // Click the joystick
    {
        OnDrag(e);
    }

    public virtual void OnPointerUp(PointerEventData e) // leave the joystick
    {
        data.InputVector = Vector3.zero; // reset joystick
        data.JoystickImage.rectTransform.anchoredPosition = Vector3.zero;
    }

    public virtual void OnDrag(PointerEventData e) // drag the joystick
    {
        Vector2 position;
        if (RectTransformUtility.ScreenPointToLocalPointInRectangle(data.JoystickBackgroundImage.rectTransform, e.position, e.pressEventCamera, out position)) // start dragging it
        {
            position.x = (position.x / data.JoystickBackgroundImage.rectTransform.sizeDelta.x);
            position.y = (position.y / data.JoystickBackgroundImage.rectTransform.sizeDelta.y);

            data.InputVector = new Vector3(position.x * 2 + 1, 0, position.y * 2 - 1);
            data.InputVector = data.InputVector.magnitude > 1 ? data.InputVector.normalized : data.InputVector;

            data.JoystickImage.rectTransform.anchoredPosition = new Vector3(
                data.InputVector.x * (data.JoystickBackgroundImage.rectTransform.sizeDelta.x / 3),
                data.InputVector.z * (data.JoystickBackgroundImage.rectTransform.sizeDelta.y / 3));
        }
    }

    public float GetHorizontalInput()
    {
        return data.InputVector.x;
    }

    public float GetVerticalInput()
    {
        return data.InputVector.z;
    }
}

如果有人可以在这里帮助我,那将很棒.当我尝试使用操纵杆时,什么也没发生.

Would be awesome, if someone could help me out here. When I try to use the joystick just nothing happens..

推荐答案

您的场景很可能缺少EventSystem.

Your scene is likely missing the EventSystem.

要创建事件系统,请转到 GameObject ---> UI ---> EventSystem .

To create the EventSystem, go to GameObject ---> UI ---> EventSystem.

它将创建一个EventSystem GameObject并将EventSystemStandalone Input Module脚本附加到它.现在应该调用回调函数,否则将Debug.Log放在其中,以确保您的语句正确.

It will create an EventSystem GameObject and attach the EventSystem and Standalone Input Module scripts to it. The callback functions should now be called otherwise put Debug.Log inside them to make sure that your statement is correct.

这篇关于Unity虚拟游戏杆不响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 10:39