当前位置:网站首页>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/
这位道友请留步️,我观你气度不凡,谈吐间隐隐有王者霸气,日后定有一番大作为!!!旁边有点赞收藏今日传你,点了吧,未来你成功️,我分文不取,若不成功️,也好回来找我。
边栏推荐
- Conglin environmental protection IPO meeting: annual profit of more than 200million to raise 2.03 billion
- Research Report on China's new energy technology and equipment market competition analysis and marketing strategy suggestions 2022-2028
- China coated abrasive tools industry market depth analysis and development strategy consulting report 2022-2028
- Application runtime layotto enters CNCF cloud native panorama
- 数据治理,说起来容易,做起来难
- Audio orchestrator: orchestrate immersive interactive audio using multiple devices
- Global and Chinese flame retardant ABS industry development trend and market demand analysis report 2022 ~ 2028
- HLS. JS: past, present and future
- QT learning setting executable exe attribute (solving the problem of Chinese attribute garbled)
- How to use the find command
猜你喜欢

Créer le premier site Web avec idea

2022-2028 global transmission type photoelectric circuit breaker industry research and trend analysis report

圖解棧幀運行過程

2022-2028 global TFT touch screen industry research and trend analysis report

Tiger Dao VC products are officially launched, a powerful supplement to seektiger ecology
What is the difficulty of the form tool that costs billions of dollars? Exclusive interview with si no

2022-2028 global horizontal reciprocating compressor industry research and trend analysis report

聊聊Adapter模式

Use apiccloud AVM multi terminal component to quickly realize the search function in the app

聊聊Adapter模式
随机推荐
图解栈帧运行过程
Conglin environmental protection IPO meeting: annual profit of more than 200million to raise 2.03 billion
Analysis of China's tractor manufacturing and operation situation and forecast report of prospect trend 2022-2028
Research on depth image compression in YUV420 color space
Nacos source code analysis 01 code structure
Why is BeanUtils not recommended?
SSH modifies grub in heiqunhui ds918+ system 7.0.1 cfg
2022-2028 global co extrusion production line industry research and trend analysis report
How to open a futures account safely at present? Which futures companies are more reliable?
Does jQuery cache any selectors- Does jQuery do any kind of caching of “selectors”?
Analysis report on scale investigation and investment development suggestions of China's special equipment inspection and testing industry 2022-2028
QT learning setting executable exe attribute (solving the problem of Chinese attribute garbled)
Research and Analysis on the status quo of China's Cross lamp market and forecast report on its development prospect (2022)
2022-2028 global cloud based remote browser isolation industry research and trend analysis report
Analysis report on market demand situation and investment direction of China's optical transmission equipment industry from 2022 to 2028
Factorymethod factory method
Lecture 14 of the Blue Bridge Cup -- number theory [example]
2022-2028 global open source cloud storage industry research and trend analysis report
NARI radar's IPO meeting: it plans to raise nearly 1billion yuan. Bao Xiaojun and his wife are Canadians
China bed and mattress market status research analysis and development prospect forecast report (2022)