0

不知道为什么会发生这种情况,但此代码允许在重新加载后发射超过 3 颗子弹。我试图找出原因。我认为这可能是检查哪个问题之间的时间,但我可能是错的。

任何帮助,将不胜感激。

public bool isFiring;
public bool isReloading = false; 
 public BulletController bullet; // Reference another script
 public float bulletSpeed; // bullet speed
 public float timeBetweenShots; // time between shots can be fired
 private float shotCounter;
 public Transform firePoint;
 public static int ammoRemaining = 3;
 public static int maxAmmo = 3;
 public Transform ammoText;
 // Use this for initialization
 void Awake () {
     isReloading = false;
     ammoRemaining = maxAmmo;
 }

 // Update is called once per frame
 void Update () {
     if(isFiring == true )
     {
         shotCounter -= Time.deltaTime;
         if(shotCounter <= 0 && ammoRemaining > 0 && isReloading == false)
         {
             shotCounter = timeBetweenShots;
             BulletController newBullet = Instantiate(bullet, firePoint.position, firePoint.rotation) as BulletController; // creates a new instance of the bullet
             newBullet.speed = bulletSpeed;
             ammoRemaining -= 1;
             ammoText.GetComponent<Text>().text = "Ammo:" + ammoRemaining;
         }

 }
 else if (ammoRemaining == 0)
 {
     StartCoroutine(Reload());
 }
 else
 {
     shotCounter = 0;
 }
 }
 public IEnumerator Reload()
 {
     isReloading = true;
     ammoText.GetComponent<Text>().text = "REL...";
     yield return new WaitForSeconds(2);
     ammoRemaining = maxAmmo;
     isReloading = false;
     ammoText.GetComponent<Text>().text = "Ammo:" + ammoRemaining;
 }
4

2 回答 2

2

这是一个题外话,不是为了解决你的问题,而是为了避免你未来的问题,特别是性能问题。如果您打算开发 3D 射击游戏,我建议您不要实例化子弹(除非您打算创建一些延时场景)

原因有两个:

  • 您需要每秒实例化和销毁许多游戏对象
  • 如果你给子弹一个真实的速度,它不应该在场景中看到

我认为为您实例化子弹的原因主要有以下两个:

  • 当你按下射击时,创造一些视觉效果,让枪管快速离开
  • OnCollisionEnter()检测你是否用一个或类似的东西击中你的目标

所以我可以给你一些你可以尝试的技巧,而不是实例化子弹。

1-为了代表拍摄,您可以在枪管末端安装一个灯,拍摄时会闪烁。您可以在 Inspector 中选择此灯的颜色、长度、强度……。

在以下脚本中,您有一个示例,说明如何在拍摄期间激活和停用灯光效果。它还控制拍摄之间的时间流逝。没有任何协程。

public Light gunLight; 
public float timeBetweenBullets = 0.15f;

void Update ()
{
    // Add the time since Update was last called to the timer.
    timer += Time.deltaTime;

    // If the Fire1 button is being press and it's time to fire...
    if(Input.GetButton ("Fire1") && timer >= timeBetweenBullets)
    {
        Shoot ();
    }

    // If the timer has exceeded the proportion of timeBetweenBullets that the effects should be displayed for...
    if(timer >= timeBetweenBullets * effectsDisplayTime)
    {
        DisableEffects ();
    }
}

public void DisableEffects ()
{
    // Disable the line renderer and the light.
    gunLight.enabled = false;
}

void Shoot (){
    timer = 0f;
    // Enable the light.
    gunLight.enabled = true;

}

2-第二部分是如何检测玩家是否朝正确的方向射击。为了解决这个问题,您需要在拍摄时使用 raycast,并分析射线击中的内容。

为此,您应该使用以下几行修改上面的 shot 方法:

void Shoot ()
    {
        // Reset the timer.
        timer = 0f;

        // Enable the light.
        gunLight.enabled = true;

        // Set the shootRay so that it starts at the end of the gun and points forward from the barrel.
        shootRay.origin = transform.position;
        shootRay.direction = transform.forward;

        // Perform the raycast against gameobjects on the shootable layer and if it hits something...
        if(Physics.Raycast (shootRay, out shootHit, range, shootableMask))
        {
            // Try and find an EnemyHealth script on the gameobject hit.
            EnemyHealth enemyHealth = shootHit.collider.GetComponent <EnemyHealth> ();

            // If the EnemyHealth component exist...
            if(enemyHealth != null)
            {
                // ... the enemy should take damage.
                enemyHealth.TakeDamage (damagePerShot, shootHit.point);
            }

            // Set the second position of the line renderer to the point the raycast hit.
            gunLine.SetPosition (1, shootHit.point);
        }
        // If the raycast didn't hit anything on the shootable layer...
        else
        {
            // ... set the second position of the line renderer to the fullest extent of the gun's range.
            gunLine.SetPosition (1, shootRay.origin + shootRay.direction * range);
        }
    }

现在,您的 shoot 方法投射一条射线,以防射线击中敌人(即标记为敌人的 GameObject),它将执行一些操作。在这种特殊情况下,我假设敌人已经附加了一个脚本来减少它被射击时的生命。

您可以使镜头的特殊效果更加复杂,例如:添加声音、渲染线条、一些粒子系统来模拟粉末……

您可以并且我认为您应该从 Unity 官方网站查看本教程,以更好地理解我在回答中刚刚提到的内容,并为您的游戏获得一些额外的想法:

https://unity3d.com/learn/tutorials/projects/survival-shooter/harming-enemies?playlist=17144

于 2017-08-30T11:52:10.627 回答
1

问题(如所讨论的)是您的 Reload() 协程在您发射最后一枪之后和释放 MouseButton(0) 之前在 Update() 内被调用了额外的时间。为避免这种情况,我建议在启动协程之前检查以确保您尚未重新加载:

 else if (ammoRemaining == 0 && !isReloading)
 {
     StartCoroutine(Reload());
 }
于 2017-08-29T20:50:21.220 回答