Unity基础篇-----Input输入

前言

此篇为个人对此部分所掌握知识的理解,如有错误的地方请谅解!,后续会对此部分知识进行进一步完善。

在程序中输入系统是必不可缺的,这部分知识也是我们必须需要掌握的,在Unity使用Input类来读取传统游戏中输入的轴,下面我们对Unity Input类中会常用到的函数及变量进行一个讲述。

一、读取键盘输入

方法所用参数:string类型(键盘按键对应的英文名称,26个英文字母可以用小写),或者使用KeyCode枚举,选择你需要判断的按键。

1、Input.GetKey()方法,返回类型为bool类型,当你一直按着某个按键时触发里面的内容

2、Input.GetKeyDown()方法,当你在按下某个按键的瞬间触发。

3、Input.GetKeyUp()方法,当你在按下某个键后松开的瞬间触发。

使用方法:

    void Update()
    {
        if (Input.GetKey("w"))
        {
            print("w键被一直按着");
        }
        if (Input.GetKey(KeyCode.W))
        {
            print("w键被一直按着");
        }
//------------------------------------
        if (Input.GetKeyDown("w"))
        {
            print("你按下了w键");
        }
        if (Input.GetKeyDown(KeyCode.W))
        {
            print("你按下了w键");
        }
 //------------------------------------
        if (Input.GetKeyUp("w"))
        {
            print("你松开了w键");
        }
        if (Input.GetKeyUp(KeyCode.W))
        {
            print("你松开了w键");
        }
     }

二、鼠标输入

方法所用参数:0(左键),1(右键),2(中键)

1、Input.GetMouseButton()方法,当你一直按着鼠标时触发。

2、Input.GetMouseButtonDown()方法,当你按下鼠标按键的一瞬间触发。

3、Input.GetMouseButtonUp()方法,当你按下鼠标后,松开的瞬间触发。

使用方法:

  void Update()
  {
      if (Input.GetMouseButton(0))
      {
          print("鼠标左键被你一直按下");
      }
      if (Input.GetMouseButtonDown(0))
      {
          print("你按下了鼠标左键");
      }
      if (Input.GetMouseButtonUp(0))
      {
          print("你松开了了鼠标左键");
      }
  }

三、虚拟按钮输入

方法所用参数:"虚拟按钮名称"

Input.GetButton()方法,当你一直按下虚拟按钮,比如手柄的扳机键(“Fire1”)

Input.GetButtonDown()方法,当你按下虚拟按钮的瞬间

Input.GetButonUp()方法,当你松开虚拟按钮的瞬间

    void Update()
    {
        if (Input.GetButton("Fire1"))
        {
            print("子弹在不断发射");
        }
        if (Input.GetButtonDown("Fire1"))
        {
            print("子弹发射");
        }
        if (Input.GetButtonUp("Fire1"))
        {
            print("停止发射");
        }
    }

四、常用到的一些方法

1、Input.GetAxis()方法,获取虚拟轴的值,返回值为float类型。值区间为[-1,1]

参数(“Horizontal”)表示左右的值,值为-1时,表示你在一直按下左方向对应的按键、设备类型为手柄时,表示你把推杆推到了最左边,为0时为默认位置,手柄中对应正中间,为1时与-1时对应的情况正好相反。

参数(“Vertical”)表示的是上下的值,与上述中左右情况类似。

通常这两个参数结合起来可用于实现,按下WASD/箭头按键/鼠标手柄,控制物体的前后左右移动等。

参数("Mouse X")返回鼠标左右移动的距离,值区间[-1,1]。

参数("Mouse Y")返回鼠标上下移动的距离,值区间[-1,1]。

实例:使用Input.GetAxis()方法实现,人物前后左右移动功能,移动鼠标控制镜头左右旋转。

public class Player : MonoBehaviour
{
    private CharacterController characterController;//角色控制器
    private float movespeed=5;//移动速度
    private float rotatespeed=2;//旋转速度
    private float rotatey = 0;//鼠标上下移动距离
    private float rotatex = 0;//鼠标左右移动距离
    private float maxangle = 45;//视角上下最大旋转角度
    private float minangle = -45; //视角上小最大旋转角度
    // Start is called before the first frame update
    void Start()
    {
        characterController = GetComponent<CharacterController>();
    }

    void Update()
    {
        float X = Input.GetAxis("Horizontal");//存储左右值
        float Y = Input.GetAxis("Vertical");//存储上下值
        Vector3 vector3 = new Vector3(X, 0, Y);//只修改x和z,y值为高度
        characterController.Move(characterController.transform.TransformDirection(vector3 * movespeed * Time.deltaTime));//调用角色控制器移动方法
        rotatex += Input.GetAxis("Mouse X") * rotatespeed;//获取鼠标左右移动距离
        rotatey -= Input.GetAxis("Mouse Y") * rotatespeed;//获取鼠标上下移动距离
        rotatey = Mathf.Clamp(rotatey, minangle,maxangle);//限制旋转角度值
        transform.localEulerAngles = new Vector3(0, rotatex, 0);//修改自身的左右旋转
        Camera.main.transform.localEulerAngles = new Vector3(rotatey,0, 0);//修改摄像机的上下旋转

    }
}

2、Input.GetTouch()方法,主要用于移动端开发使用,此方法主要获取触摸屏幕的操作数据,首先通过Input.touchCount这个方法获取到手指触摸屏幕的次数,通过GetTouch方法传输触摸次数,调用Touch结构判断手指的状态来是否为设定的状态,符合的话则执行相关内容。具体使用可参考链接:官网

  • 9
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值