键盘输入的几种情况:
1.键盘按下:Input.GetKeyDown(KeyCode.A)
2.键盘弹起:Input.GetKeyUpCode.A)
3.键盘按下持续中:Input.GetKey(KeyCode.A)
//上面这些就是API,unity帮我们写好的功能就是API
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class L4_2Demo : MonoBehaviour
{
void Start()
{
}
void Update()
{
//这节课会实现四种输入方式:
/*
* 1.蓄力
* 2.按下
* 3.按下中
* 4.弹起
*/
// 按下(只有第一次按下的那一帧才有效果,如果后面一直按了A则没有效果)
Input.GetKeyDown(KeyCode.A);
//KeyCode是一个枚举类型,这条语句返回值是一个bool值
//弹起(只有第一次弹起的那一帧才有效果)
Input.GetKeyUp(KeyCode.A);
Input.GetKeyUP("b");//也可以用字符串传入,不过实现的时候,还是会转化为枚举类型
//按下中(持续按)
Input.GetKey(KeyCode.A);
}
}
还可以让玩家修改按键,实现攻击:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public class L4_2Demo : MonoBehaviour
{
public string att;
void Start()
{
}
void Update()
{
if(Input.GetKeyDown(att))
{
print("攻击");
}
}
}
蓄力:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public class L4_2Demo : MonoBehaviour
{
public string att;
private float ak=0;
void Start()
{
}
void Update()
{
if (Input.GetKeyDown(att))
{
print("开始蓄力");
ak = 0;//每次蓄力,之前的攻击肯定要清零
}
if (Input.GetKey(att))
{
print("蓄力中");
ak += Time.deltaTime;//渲染一帧花的时间
}
if(Input.GetKeyUp(att))
{
print("攻击:" + ak);
}
}
}