当前位置:首页 > 通信资讯 > 正文

unity获取按键(unity获取键盘输入)

获取当前键盘按键,代码如下:

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 using unityengine; using system.collections; public class getcurrentkey : monobehaviour { keycode currentkey; void start () { currentkey = keycode.space; } void ongui() { if (input.anykeydown) { event e = event.current; if (e.iskey) { currentkey = e.keycode; debug.log("current key is : " + currentkey.tostring()); } } } }

下面给大家介绍unity3d鼠标、键盘的基本操作

键盘:

getkey 当通过名称指定的按键被用户按住时返回true
getkeydown 当用户按下指定名称的按键时的那一帧返回true。
getkeyup 在用户释放给定名字的按键的那一帧返回true。
getaxis(“horizontal")和getaxis(“verical”) 用方向键或wasd键来模拟-1到1的平滑输入

键盘判断:

if(input.getkeydown(keycode.a)){//keycode表示包含键盘所有键
print(“按下a键”); } if(input.getkeyup(keycode.d)){//当按d键松开时
print(“松开d键”); } if(input.getaxis(“horizontal")){//当按下水平键时
print(“按下水平键”); } if(input.getkeyup("verical“)){当按下垂直键时
print(“按下垂直键”); }

鼠标:

getbutton 根据按钮名称返回true当对应的虚拟按钮被按住时。
getbuttondown 在给定名称的虚拟按钮被按下的那一帧返回true。
getbuttonup 在用户释放指定名称的虚拟按钮时返回true。

鼠标判断:

if(input.getbutton("fire1")){//fire1表示按下鼠标左键
print(“按下鼠标左键”); } if (input.getmousebutton(0)) {//0表示鼠标左键
debug.log("按下鼠标左键"); } if (input.getmousebutton(1)) {//1表示鼠标右键
debug.log("按下鼠标右键"); } if (input.getmousebutton(2)) {//2表示鼠标中键
debug.log("按下鼠标中键"); }

给物体施加普通力:

1、先给物体添加刚体
2、transform.rigidbody.addforce(0,0,1000); 一个简单例子让小球撞破墙:

unity获取按键(unity获取键盘输入)

代码如下:

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 using unityengine; using system.collections; public class cube : monobehaviour { // use this for initialization void start () { } // update is called once per frame void update () { if(input.getkey(keycode.w)){//当鼠标按下w键时,小球向前移动 transform.translate(vector3.forward); } if(input.getkey(keycode.s)){当鼠标按下s键时,小球向后移动 transform.translate(vector3.back); 天猫双十一活动 } if(input.getkey(keycode.a)){当鼠标按下a键时,小球向左移动 transform.translate(vector3.left); } if(input.getkey(keycode.d)){当鼠标按下d键时,小球向右移动 transform.translate(vector3.right); } if(input.getbutton("fire1")){//当点击鼠标左键时,小球撞塌墙 transform.rigidbody.addforce(0,0,200);//物体向前移动的力为200 } } }

如果您对该产品感兴趣,请填写办理(客服微信:xiaoxiongyidong)

为您推荐:

发表评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。