我有一个游戏对象(HandGun),我在游戏的某个时刻禁用(setActive(false))。HandGun 附有一个名为 GunController 的脚本,它负责每次按下扳机时进行射击。
问题是,当我禁用 HandGun 时,我仍然可以射击并看到子弹从无到有,因为 HandGun 游戏对象已成功消失。
枪控制器脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using EZEffects;
public class GunController : MonoBehaviour {
public GameObject controllerRight;
public AudioClip clip;
AudioSource sound;
public int damage;
private SteamVR_TrackedObject trackedObj;
public SteamVR_Controller.Device device;
private SteamVR_TrackedController controller;
public EffectTracer TracerEffect;
public EffectImpact ImpactEffect;
public Transform muzzleTransform;
// Use this for initialization
void Start () {
sound = gameObject.AddComponent<AudioSource>();
controller = controllerRight.GetComponent<SteamVR_TrackedController>();
controller.TriggerClicked += TriggerPressed;
trackedObj = controllerRight.GetComponent<SteamVR_TrackedObject>();
device = SteamVR_Controller.Input((int)trackedObj.index);
}
private void TriggerPressed(object sender, ClickedEventArgs e)
{
shootWeapon();
}
public void shootWeapon()
{
sound.PlayOneShot(clip,0.2f);
RaycastHit hit = new RaycastHit();
Ray ray = new Ray(muzzleTransform.position, muzzleTransform.forward);
device.TriggerHapticPulse(3999);
TracerEffect.ShowTracerEffect(muzzleTransform.position, muzzleTransform.forward, 250f);
if(Physics.Raycast(ray, out hit, 5000f))
{
if (hit.collider.attachedRigidbody)
{
Enemy enemy = hit.collider.gameObject.GetComponent<Enemy>();
if (enemy)
{
enemy.TakeDamage(damage);
}
ImpactEffect.ShowImpactEffect(hit.transform.position);
}
}
}
// Update is called once per frame
void Update () {
}
}
禁用 HandGun 游戏对象的 Inspector 脚本的一部分:
public void showShop()
{
shop.SetActive(true);
shopActive = true;
if (actualGun == null)
{
actualGun = handGun;
}
actualGun.SetActive(false);
model.SetActive(true);
}
而且,如果我在游戏运行的时候手动去激活GunController脚本,我仍然可以射击,这我绝对不明白。我正在使用可以在统一商店中找到的 EZEffect。
我究竟做错了什么?我应该怎么办?
无论如何,提前感谢您的帮助!