Unity中使用摇杆控制
2016-03-22 17:51
651 查看
Unity中使用摇杆控制
本文章由cartzhang编写,转载请注明出处。 所有权利保留。文章链接:/article/4642116.html
作者:cartzhang
一、说起摇杆
XBox的摇杆控制器,不知道何种原因,它拒绝在我电脑上工作。别人给安装驱动,他们都好好的,在这里就是不识别。
只找到了一个数动的就和着使用。
为啥要研究摇杆啊?
因为这边在VR开发中,每次使用VR设置启动的时间较长,又因为有各种硬件,有枪
头盔等一批硬件控制和按钮,所以就想使用摇杆按键来在测试时候代替。
下面就是在Unity中使用摇杆的一些代码。
二、键值
摇杆的键值,在网上搜的:按钮绘制:按钮灵敏度(Button Sensitivity)为1000; A = button 0 B = joystick button 1 X = joystick button 2 Y = joystick button 3 LB = joystick button 4 RB = joystick button 5 Back = joystick button 6 Start = joystick button 7 Left Analogue Press = joystick button 8 Right Analogue Press = joystick button 9 坐标轴绘制:模拟坐标轴灵敏度(Analog Axis sensitivity)为1,量化后可以为1000; Left Analog Horizontal = X Axis Left Analog Vertical = Y Axis Triggers = 3rd Axis (Left: -1 - 0, Right: 0 - 1) _ (Left Trigger Axis: 9th, Right Trigger Axis: 10th (Both axis are 0-1)) Right Analog Horizontal = 5th axis Right Analog Vertical = 4th axis D-Pad Horizontal = 6th Axis D-Pad Vertical = 7th Axis
做为程序,怎么使用代码获取呢?
简单:
using UnityEngine; using System.Collections; public class GetKeyValue : MonoBehaviour { public float speed = 10.0F; public float rotationSpeed = 100.0F; void Update() { detectPressedKeyOrButton(); } public void detectPressedKeyOrButton() { foreach (KeyCode kcode in System.Enum.GetValues(typeof(KeyCode))) { if (Input.GetKeyDown(kcode)) Debug.Log("KeyCode down: " + kcode); } } }
把此代码挂载到任意对象上,然后按下按键看看console中的打印,就知道对应的键值了。
三、控制对象
使用摇杆来控制对象,因为这边在VR开发中,每次使用VR设置启动的时间较长,又因为有各种硬件,有枪头盔等一批硬件控制和按钮,所以就想使用摇杆按键来在测试时候代替。解放不够用的双手,之前的测试一个人不能完成,需要旋转头盔,需要按下按键,需要鼠标点击,真是需要个多的手啊!!就是这样!!
一个摇杆控制旋转和移动。
这里面,摇杆向前推,控制对象就向前移动,而左右就是控制左右旋转了。有点不符合常理,但是大家若有用就稍微修改下吧
using UnityEngine; using System.Collections; public class StickCameraControl : MonoBehaviour { public float speed = 10.0F; public float rotationSpeed = 100.0F; void Update() { float translation = Input.GetAxis("Vertical") * speed; float rotation = Input.GetAxis("Horizontal") * rotationSpeed; if (Mathf.Abs(translation) <= 0.1*speed) { translation = 0; rotation *= Time.deltaTime; transform.Rotate(0, rotation, 0); } else { translation *= Time.deltaTime; transform.position +=Camera.main.transform.forward * translation; } } }
用来控制枪的旋转和开火的代码。
枪不能自己移动的,只可以旋转。所以代码就如下:
using UnityEngine; using System.Collections; public class StickGunContorl : MonoBehaviour { public float speed = 10.0F; public float rotationSpeed = 100.0F; public KeyCode []FireButton; void Update() { SetFire(); SetRotate(); } void SetFire() { if (Input.GetKeyDown(KeyCode.JoystickButton4) || Input.GetKeyDown(KeyCode.JoystickButton5) || Input.GetKeyDown(KeyCode.Joystick1Button4) || Input.GetKeyDown(KeyCode.Joystick1Button5)) { Debug.Log(Input.mousePosition); } } void SetRotate() { //float translation = Input.GetAxis("RightV") * speed; float rotation = Input.GetAxis("RightH") * rotationSpeed; Debug.Log("gun" + rotation); rotation *= Time.deltaTime; transform.Rotate(0, rotation, 0); } }
代码超级简单。注释没有!!!
要是直接复制代码,运行会报错。原因在于你没有RightH这个东动
怎么来的呢?看这里,看这里……
这在里定义的Input,然后才可以在代码中正确调用的。
四、参考
http://tieba.baidu.com/p/3668735969——–THE——————END———————–
若有问题,请随时联系!!
非常感谢
相关文章推荐
- unity,List元素第一个成员最好是string类型
- Unity3D
- Unity3D UGUI之DoTweenAnimation脚本控制动画方法
- Unity3D UGUI之ScrollView弹簧效果
- Unity 截屏,WWW加载图片,保存到本地
- Unity3D中uGUI事件系统简述及使用方法总结
- unity ngui学习(五)
- unity Android 打包后读取 xml 文件
- unity3D OnTriggerEnter和OnCollisionEnter的一点个人心得(主要讲区别)
- 动画状态机Animator-Unity3d
- Ubuntu Unity Tweak tool 启动报错 “com.canonical.unity.webap
- unity限制目标物旋转角度
- unity ngui学习(四)
- Unity3D中JavaScript与C#对比
- Unity5.3——UI之Canvas
- Unity Shaders and Effects Cookbook (2-1) 修改 UV 坐标实现纹理贴图的滚动 模拟水流效果
- [Unity] 查找资源
- [Unity3d]打包Assetbundle并加载
- unity3d 状态存储 PlayerPrefs高级用法
- Unity学习踩坑