是否可以从脚本中选择并移动屏幕上的操纵杆?

发布于 2025-02-12 05:10:11 字数 346 浏览 1 评论 0原文

我尝试通过触摸以编程方式添加操纵杆。带有连接的屏幕棒游戏对象的预制将进行实例化,如果我释放此手指,则游戏对象将被销毁。唯一的问题是,我无法移动操纵杆。有没有办法让操纵杆对实例化操纵杆预制的手指负责?

我尝试了这一点:但是没有运气:

 ExecuteEvents.Execute(leftJoyStick.GetComponent<OnScreenStick>().gameObject, 
                         new BaseEventData(EventSystem.current), ExecuteEvents.selectHandler);

请帮忙。

I try to add a Joystick programmatically by a touch. The prefab with the attached On-Screen Stick gameobject get instantiated and if I release this finger, the gameobject get destroyed. The only problem is, I cannot move the joystick. Is there a way to make the joystick responsible to the finger who instantiated the joystick prefab?

i tried this: but with no luck:

 ExecuteEvents.Execute(leftJoyStick.GetComponent<OnScreenStick>().gameObject, 
                         new BaseEventData(EventSystem.current), ExecuteEvents.selectHandler);

please help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凉城 2025-02-19 05:10:11

好吧,我有点愚蠢。我不需要操纵杆,以这种方式吼叫。
当我触摸屏幕的右侧时,我可以环顾四周,当我触摸屏幕的左侧时,我可以在没有静态操纵杆的情况下四处走动。

private bool hasLeftStick = false;
private Touch joystickLeftToch;
private Vector2 leftStickTouchStartPos;

private bool hasRightStick = false;
private Touch lookFinger;
private Vector2 rightStickTouchStartPos;

// used in Update()
private void ObserveTouches()
{
    foreach (var touch in Input.touches)
    {
        if (Camera.main.ScreenToViewportPoint(touch.position).x > 0.5f)
        {
            if (!hasRightStick)
            {
                hasRightStick = true;
                lookFinger = touch;
                rightStickTouchStartPos = touch.position;
            }
        }
        else
        {
            if (!hasLeftStick)
            {
                hasLeftStick = true;
                joystickLeftToch = touch;
                leftStickTouchStartPos = touch.position;
            }
        }

        if(joystickLeftToch.fingerId == touch.fingerId && touch.phase == UnityEngine.TouchPhase.Moved)
        {
            Vector2 dir = (touch.position - leftStickTouchStartPos).normalized;
            direction = new Vector3(dir.x, 0f, dir.y);
        }

        if (joystickLeftToch.fingerId == touch.fingerId && touch.phase == UnityEngine.TouchPhase.Ended)
        {
            direction = Vector3.zero;
            hasLeftStick = false ;
        }

        if(lookFinger.fingerId == touch.fingerId && touch.phase == UnityEngine.TouchPhase.Moved)
        {                
            look = touch.deltaPosition;
            thridPersonCam.m_XAxis.m_InputAxisValue = look.x;
            thridPersonCam.m_YAxis.m_InputAxisValue = look.y;
        }

        if (lookFinger.fingerId == touch.fingerId && touch.phase == UnityEngine.TouchPhase.Ended)
        {
            look = Vector2.zero;
            thridPersonCam.m_XAxis.m_InputAxisValue = 0f;
            thridPersonCam.m_YAxis.m_InputAxisValue = 0f;
            hasRightStick = false;
        }
    }

    if (Input.touchCount == 0)
    {
        lookFinger.fingerId = -1;
        joystickLeftToch.fingerId = -1;
    }

}

Ok i'm kinda stupid. i don't need a joystick and do it this way bellow.
when i touch the right side of screen i can look around, when i touch the left side of screen i can move around without a static joystick.

private bool hasLeftStick = false;
private Touch joystickLeftToch;
private Vector2 leftStickTouchStartPos;

private bool hasRightStick = false;
private Touch lookFinger;
private Vector2 rightStickTouchStartPos;

// used in Update()
private void ObserveTouches()
{
    foreach (var touch in Input.touches)
    {
        if (Camera.main.ScreenToViewportPoint(touch.position).x > 0.5f)
        {
            if (!hasRightStick)
            {
                hasRightStick = true;
                lookFinger = touch;
                rightStickTouchStartPos = touch.position;
            }
        }
        else
        {
            if (!hasLeftStick)
            {
                hasLeftStick = true;
                joystickLeftToch = touch;
                leftStickTouchStartPos = touch.position;
            }
        }

        if(joystickLeftToch.fingerId == touch.fingerId && touch.phase == UnityEngine.TouchPhase.Moved)
        {
            Vector2 dir = (touch.position - leftStickTouchStartPos).normalized;
            direction = new Vector3(dir.x, 0f, dir.y);
        }

        if (joystickLeftToch.fingerId == touch.fingerId && touch.phase == UnityEngine.TouchPhase.Ended)
        {
            direction = Vector3.zero;
            hasLeftStick = false ;
        }

        if(lookFinger.fingerId == touch.fingerId && touch.phase == UnityEngine.TouchPhase.Moved)
        {                
            look = touch.deltaPosition;
            thridPersonCam.m_XAxis.m_InputAxisValue = look.x;
            thridPersonCam.m_YAxis.m_InputAxisValue = look.y;
        }

        if (lookFinger.fingerId == touch.fingerId && touch.phase == UnityEngine.TouchPhase.Ended)
        {
            look = Vector2.zero;
            thridPersonCam.m_XAxis.m_InputAxisValue = 0f;
            thridPersonCam.m_YAxis.m_InputAxisValue = 0f;
            hasRightStick = false;
        }
    }

    if (Input.touchCount == 0)
    {
        lookFinger.fingerId = -1;
        joystickLeftToch.fingerId = -1;
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文