12.unity使用者输入(GetKey与GetButton)

今天试着了解如何让使用者输入,操作Input的形式,之後就能够利用Input的参数和碰撞触发的事件进行使用者互动,触发各式各样的游戏事件。以下程序码  //变数定义  的部分在撰写程序时不用写。

输入的硬体

键盘、滑鼠、游戏摇杆、触控萤幕、麦克风......等等。

取得输入

之前在角色移动中有写到
Input.GetKey("a"),如果按a键,就会执行往上的位移。

if (Input.GetKey("a"))
     {
        this.gameObject.transform.Translate(new Vector2(-5, 0) * Time.deltaTime);
     }  

由例子中的if用法可以知道,Input.GetKey()是一个布林值,成立(输入)就会true。
接收输入主要有两种写法:GetKeyGetButton

1.GetKey:

//变数定义
public static bool GetKey (string name);
public static bool GetKey (KeyCode key);

//使用方法
if(Input.GetKey("a")){} //按下a键
if(Input.GetKey(KeyCode.Mouse0)){} //按下滑鼠左键

Keycode是unity内的enumeration(列举),提供各种按键的名字,可仔细看看。不知道名字就查吧!

GetKey的好处是很直白明了,是用上非常快速,但官方仍建议使用GetButton,因为GetButton可以自订义功能按键。(在游戏中设置更改攻击按键,就是这个XD)

2.GetButton:

GetButton搭配Input Manager使用,可以自订事件的按键(非常物件导向呢)

//变数定义
public static bool GetButton (string buttonName);

//使用方法
if (Input.GetButton("Fire1")){}

建置Input Manager
Edit → Project Settings → Input Manager

可以看到Fire1这个事件下的操控,Positive Button预设是键盘left ctrl键

输入状态

GetKeyDown : 按键是否按下,按下时会回传True。

GetKeyUp: 按键是否放开,放开时会回传True。

GetKey: 按键是否持续压着,按键压着就会一直回传True。

其他一样:
GetButtonDown : 按键是否按下,按下时会回传True。
GetButtonUp: 按键是否放开,放开时会回传True。
GetButton: 按键是否持续压着,按键压着就会一直回传True。

其他补充

输入任何键

//使用方法
if(Input.anyKey){}
if(Input.anyKeyDown){}

滑鼠
1.按键编码规则,左键(0),右键(1),中键(2),附加按钮(3~6)
_________

| 0 | 2 | 1 |

_________ 我是滑鼠

|        |
|        |
_________

2.点击除了Input.GetKey(KeyCode.Mouse0),也可以写GetMouseButton

public static bool GetMouseButton (int button); //变数定义

if(Input.GetMouseButton(0)){}//使用方法

一样有三种状态
Input.GetMouseButton(0)
Input.GetMouseButtonDown(1)
Input.GetMouseButtonUp(2)

3.滑鼠座标(x,y,z)

public static Vector3 mousePosition ; //变数定义
//使用方法
Vector3 mousePos = Input.mousePosition; //返回三位数(x,y,z)

4.滚轮(0,y)
传回(0,y)的2维变数,通常会使用Input.mouseScrollDelta.y,只取y值。

public static Vector2 mouseScrollDelta ; //变数定义
 //使用方法
Vector3 pos = sphere.position;
pos.y += Input.mouseScrollDelta.y * scale;
transform.Position = pos;

读取轴

连续移动的输入除了GetKey / GetButton,也可以使用GetAxis取得X或Y轴数据来设置。

有GetAxis还有GetAxisRaw差别在於

GetAxis回传-1到1之间的浮点数,例如0.4。

GetAxisRaw只回传-1、0、1。

//变数定义
public static float GetAxis(string axisName);
public static float GetAxisRaw(string axisName);

//使用方法
//滑鼠上的Axis
float x = horizontalSpeed * Input.GetAxis("Mouse X"); //水平鼠标偏移
float v = verticalSpeed * Input.GetAxis("Mouse Y");//垂直鼠标偏移
float scroll = Input.GetAxis("Mouse ScrollWheel");//滚轮
//键盘上的Axis
float h = Input.GetAxis("Horizontal");//A、D或左右
float v = Input.GetAxis("vertical");//W、S或上下

改写移动方法,利用Time.deltaTime;调整移动速率,让每帧移动速率相同,因此选择GetAxisRaw。

void Update()
    {
        float Ypos = Input.GetAxisRaw("Horizontal") * Time.deltaTime;
				float Xpos = Input.GetAxisRaw("vertical") * Time.deltaTime;
        transform.Position = new Vector3(Xpos, Ypos, 0);
    }

更多输入功能请参阅:Input


<<:  Azure - Day6 Azure Function

>>:  【Day 9】预训练任务大改:Splinter在QA任务上的成功尝试

Day18 page fault, LRU, second chance

前言 前几天我们讲到的都是关於虚拟记忆体的资讯,包含VMA的结构,malloc() , mmap()...

【从实作学习ASP.NET Core】Day16 | 後台 | 会员的角色

建立起了会员系统,还需要更进一步帮会员加入角色设定 毕竟後台的操作如果被一般人随便进入是会引发严重的...

【Day20】 WavenetGan, BidirectionalLSTMGAN, WaveGan 钢琴音乐生成

因为之後想花一点时间分享一下 Transformer 阅读跟实作的经验,所以这篇就没写 Trans...

Day 16. Unity: 来学Implement data persistence between sessions

:D     这个部分的概念,其实跟把训练好的模型还有过程的Loss value、Metrics v...

.NET 新手 无痛入职 _ Day3 建置专案(VS2019)

步骤1. 首先我们先到Microsoft官网下载Visual Studio 2019 的社群版本(C...