直接上代码
using UnityEngine;
public class camera : MonoBehaviour
{
public bool is2D = false;//用于全方向拖动
public bool is1D_X = false;//用于类似于游戏商店的左右拖动
public bool is1D_Y = false;//用于类似于浏览器的上下拖动
public float CAM_Xspeed = 1;//速度
public float CAM_Yspeed = 1;
void Update()
{
if (Input.GetMouseButton(0))//判断鼠标是否按下
{
//判断拖动类型并移动摄像机
if (is1D_X)
{
transform.Translate(Input.GetAxis("Mouse X") * -CAM_Xspeed, 0, 0);
}
else if (is1D_Y)
{
transform.Translate(0, Input.GetAxis("Mouse Y") * -CAM_Yspeed, 0);
}
else if (is2D)
{
transform.Translate(Input.GetAxis("Mouse X") * -CAM_Xspeed, Input.GetAxis("Mouse Y") * -CAM_Yspeed, 0);
}
}
}
}