为了防止触摸互相影响

时间:2019-11-02 08:13:53

标签: c# unity3d

有操纵杆和游戏对象。

分别操作操纵杆和游戏对象即可正常工作。

但是当您尝试同时处理它们时,游戏对象的触摸会受到操纵杆触摸的影响。

我已经尝试解决这个问题超过两个星期。但是我做不到。

以下是操纵杆和游戏对象的脚本。

public class Crosshair : MonoBehaviour
{
    private Touch touch;
    private Vector3 screenSpace;
    private Vector3 offset;

    private void Update()
    {
        if(Input.touchCount > 0)
        {
            for (int i = 0; i < Input.touchCount; i++)
            {
                if (EventSystem.current.IsPointerOverGameObject(i) == false)
                {
                    touch = Input.GetTouch(i);
                    if (touch.phase == TouchPhase.Began)
                    {
                        OnFingerDown();
                    }
                    if (touch.phase == TouchPhase.Moved)
                    {
                        OnDrag();
                    }
                }
            }
        }
    }

    void OnFingerDown()
    {
        screenSpace = Camera.main.WorldToScreenPoint(transform.position);

        offset = transform.position - Camera.main.ScreenToWorldPoint
                 (new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenSpace.z));

    }

    void OnDrag()
    {
        if (Input.mousePosition.x > Screen.width / 2)
        {
            var curScreenSpace = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenSpace.z);
            var curPosition = Camera.main.ScreenToWorldPoint(curScreenSpace) + offset;

            transform.position = curPosition;

            transform.position = new Vector3(Mathf.Clamp(transform.position.x, -18, 18),
                                             Mathf.Clamp(transform.position.y, -10, 10), transform.position.z);
        }
    }
}
public class JoystickController : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IDragHandler
{
    [SerializeField] private RectTransform rect_Background;
    [SerializeField] private RectTransform rect_Joystick;

    private float radius;

    [SerializeField] private GameObject player;
    [SerializeField] private float speed;

    public bool isTouch = false;
    private Vector3 touchPos;

    void Start()
    {
        radius = rect_Background.rect.width * 0.5f;
    }

    // Update is called once per frame
    void Update()
    {
        if (isTouch)
            player.transform.position += touchPos;
    }
    public void OnDrag(PointerEventData eventData)
    {
        Vector2 value = eventData.position - (Vector2)rect_Background.position;
        value = Vector2.ClampMagnitude(value, radius);
        rect_Joystick.localPosition = value;

        float disrance = Vector2.Distance(rect_Background.position, rect_Joystick.position) / radius;
        value = value.normalized;
        touchPos = new Vector3(value.x * speed * disrance * Time.deltaTime, value.y * speed * disrance * Time.deltaTime, 0);
    }

    public void OnPointerDown(PointerEventData eventData)
    {
        isTouch = true;
    }

    public void OnPointerUp(PointerEventData eventData)
    {
        isTouch = false;
        rect_Joystick.localPosition = Vector2.zero;
        touchPos = Vector3.zero;
    }
}

ps。我使用了翻译器。 因此内容可能难以理解,对此我感到抱歉。

0 个答案:

没有答案