当前位置:网站首页>Unity技术手册 - GetKey和GetAxis和GetButton
Unity技术手册 - GetKey和GetAxis和GetButton
2022-06-25 21:48:00 【华为云】
本文约3.3千字,新手阅读需要11分钟,复习需要3分钟 【收藏随时查阅不再迷路】
关于作者
众所周知,人生是一个漫长的流程,不断克服困难,不断反思前进的过程。在这个过程中会产生很多对于人生的质疑和思考,于是我决定将自己的思考,经验和故事全部分享出来,以此寻找共鸣 !!!
专注于Android/Unity和各种游戏开发技巧,以及各种资源分享(网站、工具、素材、源码、游戏等)
有什么需要欢迎私我,交流群让学习不再孤单。

前提
这是小空坚持写的Unity新手向系列,欢迎品尝。
新手(√√√)
大佬(√)
实践过程
GetKey
GetKey 当按下和抬起键盘上对应参数的键时返回true,会瞬间触发N多次,长按会一直触发,平时不使用,但是类似按住类似开火的功能时可能用到。
GetKeyDown 当按下参数对应名字的键盘的那一帧返回true,按下瞬间仅触发一次,常用。
GetKeyUp 当抬起参数对应名字的键盘的那一帧返回true,抬起瞬间仅触发一次,常用。
上面三个方法参数均为KeyCode或者按键字符(小写),包含很多,文章最下方有按键说明。
void Update(){ if (Input.GetKey(KeyCode.W)) { print("按下键盘w"); } if (Input.GetKey("f5")) { print("按下键盘f5"); } if (Input.GetKeyDown(KeyCode.S)) { print("按下键盘s"); } if (Input.GetKeyDown("d")) { print("按下键盘d"); } if (Input.GetKeyUp(KeyCode.Q)) { print("按下键盘q"); } if (Input.GetKeyUp("tab")) { print("按下键盘tab"); }}GetAxis
返回由 axisName 标识的虚拟轴的值。返回的是float类型,在-1到1之间。
举例:默认该值为0,按住【D】或【→】会从0增长到1,最后保持1,松开后从1回归到0。其他同理。
该值与帧率无关,无序担心帧率变化。
主要是控制上下左右,常用于控制角色移动,默认是【WASD】键和小键盘左侧的【上下左右】箭头键。
print(“键盘:垂直值-1到1:”+Input.GetAxis(“Vertical”));
print(“键盘:水平值-1到1:”+Input.GetAxis(“Horizontal”));
print(“触摸:鼠标沿着屏幕X移动时触发-1到1:”+Input.GetAxis(“Mouse X”));
print(“触摸:鼠标沿着屏幕Y移动时触发-1到1:”+Input.GetAxis(“Mouse Y”));
print(“触摸:当鼠标滚动轮滚动时触发-1到1:”+Input.GetAxis(“Mouse ScrollWheel”));
默认可以传入Edit-Input Manager-中的几个输入控制。

去重后还剩下11个参数可传递:

可在Edit-Project Setting-Input中设置或修改,一般默认即可,毕竟大家都养成了一定的键盘按键习惯。
如果上面的GetAxis平滑处理不能满足你,想要自己处理平滑度,可以是用Input.GetAxisRaw函数,该函数默认不按键为0或返回为0,按键范围为-1和1。
GetButton
GetButtonDown表示鼠标按下才会执行一次
GetButton表示按下过程执行多次和按住鼠标不放就一直执行,可用于武器开火功能
GetButtonUp表示按下鼠标放开后才会执行一次
传递的都是string类型字符串,默认可以传入Edit-Input Manager-中的几个输入控制。和GetAxis参数一样。

其他
作者:小空和小芝中的小空
转载说明-务必注明来源:https://zhima.blog.csdn.net/
这位道友请留步️,我观你气度不凡,谈吐间隐隐有王者霸气,日后定有一番大作为!!!旁边有点赞收藏今日传你,点了吧,未来你成功️,我分文不取,若不成功️,也好回来找我。
边栏推荐
- Jingwei Hengrun is registered through the science and Innovation Board: it plans to raise 5billion yuan, with a 9-month revenue of 2.1 billion yuan
- Flutter 網絡請求封裝之Dio(Cookie管理、添加攔截器、下載文件、异常處理、取消請求等)
- Adaptive streaming playback statistics set
- OSPF - detailed explanation of GRE tunnel (including configuration command)
- Global and Chinese flame retardant ABS industry development trend and market demand analysis report 2022 ~ 2028
- Conglin environmental protection IPO meeting: annual profit of more than 200million to raise 2.03 billion
- Huawei cloud SMS has tested that many mobile phones prompt frequent sending
- Illustration de l'exécution du cadre de pile
- Data governance is easier said than done
- 【WPF】CAD工程图纸转WPF可直接使用的xaml代码技巧
猜你喜欢

Obsidian basic tutorial

圖解棧幀運行過程

2022-2028 global transmission type photoelectric circuit breaker industry research and trend analysis report
[invitation letter] on March 4, the platform enabled digital intelligence Innovation -- UFIDA BiP PAAS cloud platform IUAP digital intelligence hundred cities forum · Jinan Station

Exclusive interview with deepmindceo hassabis: we will see a new scientific Renaissance! AI's new achievements in nuclear fusion are officially announced today
![Lecture 14 of the Blue Bridge Cup -- number theory [exercises]](/img/96/0971909c8bf25820c2d4f520bb83fb.jpg)
Lecture 14 of the Blue Bridge Cup -- number theory [exercises]

NRM source switching tool

2022-2028 global RBI platform industry research and trend analysis report

【WPF】CAD工程图纸转WPF可直接使用的xaml代码技巧

2022-2028 global industrial TFT LCD industry survey and trend analysis report
随机推荐
Travel notes of 2022giao
图解栈帧运行过程
Yyds dry goods inventory CEPH installation visual dashboard
NARI radar's IPO meeting: it plans to raise nearly 1billion yuan. Bao Xiaojun and his wife are Canadians
Interview shock 23: talk about thread life cycle and transformation process?
AbstractFactory Abstract Factory
Analysis report on demand and investment forecast of global and Chinese flame retardant hydraulic oil market from 2022 to 2028
3.4 cloning and host time synchronization of VMware virtual machine
HotSpot JVM 「01」类加载、链接和初始化
Hard liver! Super detailed basic introduction to Matplotlib!!!
Conglin environmental protection IPO meeting: annual profit of more than 200million to raise 2.03 billion
Introduction to HNU database system ODBC
Hello, teacher, is it really safe to open an account in Alipay fund?
This 110 year old "longevity" enterprise has been planning for the next century
Flutter 網絡請求封裝之Dio(Cookie管理、添加攔截器、下載文件、异常處理、取消請求等)
Huawei cloud SMS has tested that many mobile phones prompt frequent sending
Diagram of stack frame running process
2022爱分析· IT运维厂商全景报告
Global and Chinese flame retardant ABS industry development trend and market demand analysis report 2022 ~ 2028
Créer le premier site Web avec idea