Input类
※ 包装了输入的功能类,读取数据(建议在Update里使用)
※ 鼠标类(返回true值)
bool result = Input.GetMouseButton(0);
--获取鼠标按下的数据,一直按,一直读取数据
bool result = Input.GetMouseButtonDown(0);
--获取鼠标按下的那一帧的数据
bool result = Input.GetMouseButtonUp(0);
--获取鼠标抬起的那一帧的数据
0鼠标左键 1鼠标右键 2鼠标中键
※ 键盘类
bool result = Input.GetKey(KeyCode.A);
bool result = Input.GetKeyDown(KeyCode.A);
bool result = Input,GetKeyUp(KeyCode.A);
※ 同时按下两个键盘
if(Input.GetKey(KeyCode.A) && Input.GetKeyDown(KeyCode.B))
{
Debug.Log("AB")
}
//同时按下AB键 会显示AB
狙击镜(拉近拉远效果)
其中使用到了
Mathf.Lerp();函数
※ Mathf.Lerp(当前位置,目标位置,百分比);
列Mathf.Lerp(currentCamera.fieldOfView,zoomLevel[index],0.1f)
Mathf.Abs();绝对值函数,一般配合lerp函数使用,因为lerp函数只能无限的接近;
public float[] zoomLevel;//变换的等级,用数组计数方便以后修改
//需要设置等级和 每个等级的详细参数
private int index;//当前索引
private Camera currentCamera;//创建一个相机类方便以后找索引
private void Start()
{
currentCamera = this.GetComponent<Camera>();
//初始化相机的参数
}
private void Update()
{
if(Input.GetMouseButton(1))
{
index = index + 1 % zoomLevel.Length;
//这一步是点到最大索引值然后又返回零
}
currentCamera.field of view = Mathf(currentCamera.fieldofview,zoomLeve[index],0,1f)
if(Mathf.Abs(currentCamera.fieldofview - zoomLevel[index]) <0.1f)
currentCamera.fieldofview = zoomLevel[index];
}
InputManager(虚拟轴)
※ Edit->projectSetting->Input;
※ 使用脚本通过虚拟轴获自定义键的输入
※ 玩家后期,可以根据自己的偏好来设定按键
※ 一个虚拟轴可以绑定4个真实按键
※ 返回两个正向按钮(right,d)两个负向按钮(left,a)
※ 虚拟轴可以重载的形式,加键
※ Gravity(重力) 0->1
※ Sensitivity(灵敏度) 1->0
获取虚拟轴
bool result = Input.GetButton(“虚拟轴名”);
bool result = Input.GetButtonDown(“虚拟轴名”);
bool result = Input.GetButtonUp(“虚拟轴名”);
//后面的虚拟轴名 要和Edit->projectSetting->Input
//里设置的Name一样
float value = Input.GetAxis(“虚拟轴名”)
//获取数据的形式是有小数点的形式
//-1,-0.3,-0.2,-0.1···0···,0.1,0.2,0.3,···1
float value = Input.GetAxisRaw(“虚拟轴名”)
//-1,0,1