我正在尝试制作某种射击游戏,并且有一个我要开始的脚本。这是一个创建枪的脚本,我已经可以拿起它并移动它,因为我在对象上附加了一个可投掷的脚本。我也可以发射它(我有一个发射点和一个射弹预制件)。但是,目前,唯一的方法是按下键盘上的按钮。我有一个下拉菜单,我可以选择要开枪的按钮。问题是我正试图把它做成一个 VR 游戏,所以我需要一些方法来使用我的 oculus 触摸控制器来开枪。我想知道是否有办法让控制器上的按钮像键盘上的按钮一样,或者是否有人可以告诉我如何修改我的脚本以便我可以使用 steamVR Actions 系统。
我正在使用带有 oculus 链接、连接到 steamVR 和 Unity 2019.3.1 个人版的 oculus 任务。
我的枪脚本在这里:
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);
}
}
感谢您提供任何帮助。