鼠標滑動判斷移動方向

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class zuoyoiu : MonoBehaviour
{
    private Vector2 first = Vector2.zero;
    private Vector2 second = Vector2.zero;
    void  Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            //記錄鼠標按下的位置   
            first = Input.mousePosition;
        }
        if (Input.GetMouseButtonUp(0))
        {
            //記錄鼠標拖動的位置   
            second = Input.mousePosition;
            if (second.x < first.x)
            {
                //拖動的位置的x座標比按下的位置的x座標小時,響應向左事件   
                print("left");
            }
            if (second.x > first.x)
            {
                //拖動的位置的x座標比按下的位置的x座標大時,響應向右事件   
                print("right");
            }
            first = second;
        }
       
    }
}
 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章