笔者用飞行摇杆(Hotas X)与Unity3D进行了连接,主要是使用摇杆模拟飞行游戏,自己主要在这里分享一点遇到的问题,如有错误,还望各位大佬指正。
点击打开链接(密码 revq ),这个链接是Hotas X的使用说明书。Hotas X的图片如下:
笔者只将飞行摇杆用于PC端,至于PLAYSTATION端并没有测试。首先得了解飞行摇杆的各个轴,直接上图
要通过Unity3D获得轴信息,必须先打开Edit->Project settings->Input,增加轴的数量,自定义名称,如图:
这里有几个注意事项,如图1,必须将Type选成Joystick Axis,其中Joystick的X轴对应 X axis,Y轴对应 Y axis,Z轴对应3rd Axis,RZ轴对应4th axis,Slider 轴(rocking button)对应5th axis(此轴在LED显示红色的情况下与RZ轴的功能一致,但是当LED显示绿色的情况下,自己得手动编程实现该功能)。
特别注意,各个轴的变化都是-1~1,如果发现不连续变化,记得调节3即Sensitivity为小数字。
对于飞行摇杆上的按钮与Unity3D中的对应关系,自己可以通过一段代码查看,如下:
public KeyCode GetKeyDownCode()
{
foreach (KeyCode key in Enum.GetValues(typeof(KeyCode)))
{
if (Input.GetKeyDown(key))
{
Debug.Log(key.ToString());
return key;
}
}
return KeyCode.None;
}
控制飞机飞行的代码如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System;
public class FlightControl : MonoBehaviour {
private float startSpeed = 0.02f;//初始速度
private float flySpeed = 0f;//飞行速度
private float speedFlag = 0;//飞行控制标志位
private float pitchingSpeed = 0.2f;//飞机俯仰的速度
private float rotateSpeedZ = 0.2f;//飞机绕Z轴旋转的速度
private float rotateSpeedY = 0.2f;//飞机绕Y轴旋转的速度
private Vector3 offsetPosition; //摄像机和飞机的偏移量
private Transform mainCamera;
public float distanceH = 15f;
public float distanceV = 10f;
public float smoothSpeed = 10f; //平滑参数
public float before = 0f;
private void Start()
{
mainCamera = Camera.main.transform;
offsetPosition = mainCamera.position - transform.position;
}
private void FixedUpdate()
{
FlySpeedControl();
FlyPitchingControl();
FlyOverturnControl();
FlyRotateControl();
FlyRotateControl2();
}
private void Update()
{
GetKeyDownCode();
}
private void LateUpdate()
{
Vector3 nextpos = transform.forward * -1 * distanceH + transform.up * distanceV + transform.position;
mainCamera.position = Vector3.Lerp(mainCamera.position, nextpos, smoothSpeed * Time.deltaTime); //平滑插值
//mainCamera.position = nextpos;
mainCamera.LookAt(transform);
}
#region
//利用气流阀来控制飞行的速度
private void FlySpeedControl()
{
float current = -Input.GetAxis("Accelerate");
Debug.Log(current);
//在Z轴的负方向,判断是加速(通过轴的返回信息判断)
if (current >= 0&¤t>=before)
{
//判断飞行摇杆的气流阀是否发生了位置变化
if(current!=before)
flySpeed = flySpeed + startSpeed * current;
if (flySpeed >= 0.5f)
flySpeed = 0.5f;
if (current >= 0.95f)
flySpeed = 0.5f;
transform.Translate(new Vector3(0, 0, flySpeed));
}
//在Z轴的负方向,判断是减速(通过轴的返回信息判断)
if (current >= 0 && current < before)
{
if (current != before)
flySpeed = flySpeed - startSpeed * current ;
transform.Translate(new Vector3(0, 0, flySpeed));
}
//在Z轴的正方向,判断是加速(通过轴的返回信息判断)
if (current < 0 && current >= before)
{
if (current != before)
flySpeed = flySpeed + startSpeed * Mathf.Abs(current) ;
transform.Translate(new Vector3(0, 0, flySpeed));
}
//在Z轴的正方向,判断是减速(通过轴的返回信息判断)
if (current < 0 && current < before)
{
if (current != before)
flySpeed = flySpeed - startSpeed * Mathf.Abs(current) ;
if (current <= -0.95f)
flySpeed = 0f;
transform.Translate(new Vector3(0, 0, flySpeed));
}
before = -Input.GetAxis("Accelerate"); //上一帧率的轴返回值
}
#endregion
#region
//通过操作杆的Y轴进行俯仰的控制
private void FlyPitchingControl()
{
float v = Input.GetAxis("Vertical");
transform.Rotate(new Vector3(v * pitchingSpeed,0,0));
}
#endregion
#region
// 通过操作杆的X轴进行飞机的翻转
private void FlyOverturnControl()
{
float h = Input.GetAxis("Horizontal");
transform.Rotate(new Vector3(0, 0,-h * rotateSpeedZ));
}
#endregion
#region
//通过操作杆的RZ轴进行飞机的旋转
private void FlyRotateControl()
{
float t = Input.GetAxis("Twist");
transform.Rotate(new Vector3(0, t * rotateSpeedY, 0));
}
#endregion
#region
//通过Slider Axis轴来控制飞机的旋转
private void FlyRotateControl2()
{
float s = Input.GetAxis("Slider");
}
#endregion
public KeyCode GetKeyDownCode()
{
foreach (KeyCode key in Enum.GetValues(typeof(KeyCode)))
{
if (Input.GetKeyDown(key))
{
Debug.Log(key.ToString());
return key;
}
}
return KeyCode.None;
}
private void OnGUI()
{
GUI.Label(new Rect(0,0,100,200), Math.Round(flySpeed,4).ToString());
}
}