unity 自己写了一个虚拟摇杆

自己写了一个虚拟摇杆,很简单的原型:

// 虚拟摇杆儿
    public void RotateJoyStick()
    {
        if (Input.GetMouseButtonDown(0))
        {
            joyStickBase.anchoredPosition = Input.mousePosition;
            centerStick.anchoredPosition = Input.mousePosition;

            joyStickBase.gameObject.SetActive(true);
            centerStick.gameObject.SetActive(true);

            stickInUse = true;
        }
        else if (Input.GetMouseButtonUp(0))
        {
            joyStickBase.gameObject.SetActive(false);
            centerStick.gameObject.SetActive(false);

            stickInUse = false;
        }
        else if (Input.GetMouseButton(0) && stickInUse)
        {
            centerStick.anchoredPosition = Input.mousePosition;
            float dist = Vector2.Distance(centerStick.anchoredPosition, joyStickBase.anchoredPosition);
            if (dist > joyStickRadius)
            {
                Vector2 offset = (centerStick.anchoredPosition - joyStickBase.anchoredPosition).normalized * joyStickRadius;
                centerStick.anchoredPosition = joyStickBase.anchoredPosition + offset;
            }

            // dir
            joyStickDir = centerStick.anchoredPosition - joyStickBase.anchoredPosition;
        }
    }

你可能感兴趣的:(unity,角色控制)