当前位置:网站首页>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/
这位道友请留步️,我观你气度不凡,谈吐间隐隐有王者霸气,日后定有一番大作为!!!旁边有点赞收藏今日传你,点了吧,未来你成功️,我分文不取,若不成功️,也好回来找我。
边栏推荐
- 聊聊Adapter模式
- Why absolute positioning overlaps
- 2022-2028 global extrusion coating and lamination production line industry research and trend analysis report
- Programmer weekly (issue 4): the wealth view of programmers
- 你好,请问老师,在支付宝基金开户真的安全吗?
- SSH modifies grub in heiqunhui ds918+ system 7.0.1 cfg
- 3.4 cloning and host time synchronization of VMware virtual machine
- 圖解棧幀運行過程
- Travel notes of 2022giao
- Sqlmap learning (sqli labs as an example)
猜你喜欢

Beyond natural motion: exploring the discontinuity of video interpolation

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

简单好用的缓存库 gcache

2022-2028 global cloud based remote browser isolation industry research and trend analysis report

Introduction to HNU database system ODBC
Data annotation in the second half: growth flywheel under PLG mode Manfu Technology

2022-2028 global RBI platform industry research and trend analysis report
What is the difficulty of the form tool that costs billions of dollars? Exclusive interview with si no

Tiger DAO VC产品正式上线,Seektiger生态的有力补充

Why is BeanUtils not recommended?
随机推荐
用idea建立第一個網站
Does jQuery cache any selectors- Does jQuery do any kind of caching of “selectors”?
Analysis of gpl3.0 license software copyright dispute cases
2022-2028 global RBI platform industry research and trend analysis report
Huasheng lithium battery IPO meeting: 9-month revenue of 690million; shenjinliang's family relationship is complex
China soft magnetic material market demand status and prospect scale forecast report 2022-2028
Canoe: the fifth simulation project: simulation + test
HNU network counting experiment: Experiment 5 network layer and link layer protocol analysis (packettracer)
Analysis report on market business model and development direction of China mobile operation industry from 2022 to 2028
No nonsense, code practice will help you master strong caching and negotiation caching!
Youku IPv6 evolution and Practice Guide
Tiger DAO VC产品正式上线,Seektiger生态的有力补充
MATLAB Programming Notes
TLog 助力盘古框架实现微服务链路日志追踪
再突破!阿里云进入Gartner云AI开发者服务挑战者象限
How to use Matplotlib library to realize enlarged display of graphic local data
Online crudhasone Association query reports an error unabletouseinternalvariable:list
Programmer weekly (issue 4): the wealth view of programmers
Flutter 網絡請求封裝之Dio(Cookie管理、添加攔截器、下載文件、异常處理、取消請求等)
This 110 year old "longevity" enterprise has been planning for the next century