我正在尝试做一些射击类游戏,并有一个脚本,我开始与。这是一个创建枪的脚本,我已经可以拿起它并移动它了,因为我在对象上附加了一个可抛出的脚本。我也可以发射它(我有一个发射点和一个射弹预制件)。然而,目前唯一的方法就是按下键盘上的一个按钮。我有一个下拉菜单,我可以选择我想要开枪的按钮。问题是,我试图使这是一个虚拟现实游戏,所以我需要一些方法来使用我的oculus触摸屏开枪。我想知道是否有一种方法可以让你的控制器上的按钮就像你在键盘上按下的按钮一样,或者是否有人能告诉我如何修改我的脚本,以便我可以使用steamVR Actions系统。
我使用的是连接到steamVR和Unity2019.3.1 personal的oculus quest with oculus链接。
我的Gun脚本在这里:
using UnityEngine;
using System.Collections;
public class GenericGun : MonoBehaviour {
public KeyCode fireKey = KeyCode.Mouse0;
public GameObject projectilePrefab;
public Transform launchPoint;
public bool autoFire = false;
[Tooltip("Projectile always fires along local Z+ direction of Launch Point")]
public float launchVelocity;
public float projectilesPerSecond = 1f;
public float projectileLifetime = 3f;
private float _lastFireTime = 0f;
[Header("If checked, ignore all ammo settings below")]
public bool infiniteAmmo = true;
public int startingAmmo = 10;
public int maxAmmo = 10;
public int currentAmmo;
public float ammoRechargeTime = 1f;
private float _lastAmmoRechargeTime = 0f;
void Start() {
currentAmmo = startingAmmo;
}
void Update() {
if (autoFire || Input.GetKey (fireKey))
{
Launch();
}
if (ammoRechargeTime > 0f && Time.time > _lastAmmoRechargeTime + ammoRechargeTime) {
this.AddAmmo(1);
}
}
public void Launch() {
if (currentAmmo > 0f && Time.time - _lastFireTime >= 1f / projectilesPerSecond)
{
_lastFireTime = Time.time;
// ignore removing ammo if it's infinite
if(!infiniteAmmo)
currentAmmo -= 1;
GameObject newGO;
if (launchPoint != null)
{
newGO = GameObject.Instantiate (projectilePrefab, launchPoint.position, launchPoint.rotation) as GameObject;
}
else
{
newGO = GameObject.Instantiate (projectilePrefab, this.transform.position, this.transform.rotation) as GameObject;
}
Rigidbody newRB = newGO.GetComponent<Rigidbody> ();
if (newRB != null)
{
newRB.AddRelativeForce (Vector3.forward * launchVelocity, ForceMode.VelocityChange);
}
if (projectileLifetime > 0f) {
GameObject.Destroy(newGO, projectileLifetime);
}
}
}
public void AddAmmo(int amount) {
currentAmmo = Mathf.Min(maxAmmo, currentAmmo + 1);
}
}感谢您能提供的任何帮助。
发布于 2020-06-17 17:18:33
要使用Oculus Quest控制器生成输入,您需要使用OVRInput。
若要执行此操作,还需要在场景中包含OVRManager的实例,并在处理输入的脚本上调用OVRInput.Update()/FixedUpdate()。
有关docs的更多信息。
https://stackoverflow.com/questions/62421143
复制相似问题