鼠标按键输入
bool mouse= Input.GetMouseButton(1);//0左键 1右键 2中键
if (mouse)
{
Debug.Log("一直按下鼠标的右键");
}
bool mouse0 = Input.GetMouseButtonDown(0);
if (mouse0)
{
Debug.Log("按下了鼠标左键");
}
bool mouse1 = Input.GetMouseButtonUp(2);
if (mouse1)
{
Debug.Log("抬起滚轮");
}
键盘按键输入
//获取键盘上的很纵轴
//取值范围 - 1 - 1;
float h = Input.GetAxis("Horizontal");//横轴
Debug.Log(h);
float v = Input.GetAxis("Vertical");//纵轴
Debug.Log(v);
transform.Translate(transform.forward * v * Speed * Time.deltaTime);
transform.Rotate(transform.up * h * Speed * Time.deltaTime);
例子鼠标双击
if (Input.GetMouseButtonDown(0))
{
count++;
if (count==1)
{
timer = Time.time;
}
if (count==2&&Time.time-timer<=0.5f)
{
Debug.Log("双击了6666");
count = 0;
}
if (Time.time-timer>0.5f)
{
count = 0;
}
}
获取鼠标位置
//左下角为坐标原点,右上角为最大值
//获得的是一个二维坐标(屏幕坐标)
Input.mousePosition;
//获取鼠标在屏幕上的X轴 和Y轴
Debug.Log(Input.GetAxis("Mouse X"));
Debug.Log(Input.GetAxis("Mouse Y"));
//获取鼠标滚轮
Debug.Log(Input.GetAxis("Mouse ScrollWheel"));
例子:
//获得主摄像机的视距
//Debug.Log(Camera.main.name);
float fo = CameraMain.GetComponent<Camera>().fieldOfView;
fo -= Input.GetAxis("Mouse ScrollWheel") * 12f;
fo = Mathf.Clamp(fo, 30, 100);
CameraMain.GetComponent<Camera>().fieldOfView = fo;