具体可参考:输入 - Unity 手册
1、键盘输入
void Update()
{
//蓄力Demo
//按下
//按下中
//弹起
//只有在第一次按下的那一帧有效
if (Input.GetKeyDown(AttackKeyName))
{
Debug.Log("开始蓄力");
attackValue = 0;
}
if (Input.GetKey(AttackKeyName))
{
Debug.Log("蓄力中");
attackValue += Time.deltaTime;//随着时间的增加增加攻击
}
if (Input.GetKeyUp(AttackKeyName))
{
Debug.Log("发动攻击:"+ attackValue);
}
}
注:AttackKeyName为string类型,当我们在键盘按上指定的键位之后将触发Input.GetKeyDown()(刚按下按键时)返回true,Input.GetKey(AttackKeyName)(持续按下时)返回true,Input.GetKeyUp()(松开时)返回true,而在这段代码中则是当玩家按下指定按键时积攒攻击力attackValue而在松开后打出攻击,KeyCode中包含所有的按键
2、鼠标输入
//0: 左键 1: 右键
if (Input.GetMouseButtonDown(0))
{
print("左键按下");
}
if (Input.GetMouseButton(0))
{
print("左键持续按下中");
}
if (Input.GetMouseButtonUp(0))
{
print("左键弹起");
}
//鼠标位置
//并不是游戏中的位置注:在鼠标输入中:0为左键 1为右键,Input.GetMouseButtonDown()为刚按下时,Input.GetMouseButton()为按下时,Input.GetMouseButtonUp()为松开按键时
4、InputManager
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
transform.Translate(new Vector3(h, v)*0.02f);
float wheel = Input.GetAxis("Mouse ScrollWheel");
print(wheel);注:Input Manager的预设路径Edit → Project Settings → Input Manager ,预设Horizontal、Vertical、Fire1等轴。通过Input.GetAxis()获取对应的预设值分别对应--水平(左右ad)、竖直(前后ws)、开火键(鼠标左),使用该函数返回一个-1~1的float类型的数值
(哈哈,第一次发个人博客水一下)
评论