VR自动拍摄脚本? (更改脚本)

时间:2018-10-22 16:46:46

标签: visual-studio unity3d unityscript virtual-reality

我正在将游戏集成到VR中,现在我想更改控制方式。在下面给出的脚本中,当玩家按下鼠标按钮时,枪支会发射子弹,但是现在在VR中,我不希望我的游戏由任何控制器控制。我想要的是玩家移动头部并且将焦点对准敌人时,无需按下任何按钮即可开始射击(我的意思是自动射击)。我该怎么办?

grausing System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GunScript : MonoBehaviour {

    public float damage = 10f;
    public float range = 100f;
    public float fireRate = 15f;
    public float impactForce = 30f;

    public Camera fpsCam;

    public GameObject impactEffect;

    private float nextTimeToFire = 0f;

    // Update is called once per frame
    void Update () {

        if (Input.GetButton("Fire1") && Time.time >= nextTimeToFire)
        {
            nextTimeToFire = Time.time + 1f / fireRate;
            Shoot();
        }
    }

    void Shoot()
    {
        RaycastHit hit;
        if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range))
        {
            Debug.Log(hit.transform.name);

            Target target = hit.transform.GetComponent<Target>();
            if (target != null)
            {
                target.TakeDamage(damage); 
            }

            if(hit.rigidbody != null)
            {
                hit.rigidbody.AddForce(-hit.normal * impactForce);
            }
            GameObject ImpactEffectGO = Instantiate(impactEffect, hit.point, Quaternion.LookRotation(hit.normal));
            Destroy(ImpactEffectGO, 2f);
        }
    }
}

1 个答案:

答案 0 :(得分:1)

由于您已经拥有射击技师,只需更改Update()就可以完成您想要的

void Update () {

    if (Time.time >= nextTimeToFire)
    {
        nextTimeToFire = Time.time + 1f / fireRate;
        Shoot();
    }
}