产生的子弹比允许的更多

时间:2017-08-29 20:12:40

标签: c# unity3d coroutine reloading

不确定为什么会发生这种情况,但此代码允许在重新加载后触发超过3个子弹。我试图找出原因。我想可能是检查哪个是这个问题但是我可能错了。

任何帮助都将不胜感激。

video.addEventListener("loadedmetadata", function() { 
  console.log(video.clientWidth)
}, true)

2 个答案:

答案 0 :(得分:2)

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

原因有两个:

  • 您需要每秒实例化和销毁许多GameObjects
  • 如果你给子弹一个真实的速度,就不应该看到它 现场

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

  • 创造一些离开真正快速的东西的视觉效果 按下拍摄时的桶
  • 检测您是否使用OnCollisionEnter()或类似的
  • 命中了目标

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

1 - 为了代表拍摄,您可以在镜筒的末端附加一盏灯,拍摄时会闪烁。您可以在检查器中选择此灯光的颜色,长度,强度......

在下面的脚本中,您将了解如何在拍摄期间激活和停用灯光效果的示例。它还控制射击之间的时间流逝。没有任何协程。

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 - 第二部分是如何检测玩家是否朝正确方向射击。要解决这个问题,你需要在拍摄时使用光线投射,并分析射线击中的内容。

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

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);
        }
    }

现在你的射击方法投射了一条射线,以防射线击中敌人(这是一个标记为敌人的GameObject),它会执行一些动作。在这种特殊情况下,我认为敌人已经附加了一个脚本来减少其射击时的生命。

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

你可以,我认为你应该从Unity官方网站上查看这个教程,以便更好地理解我在答案中提到的内容,并为你的游戏获得一些额外的想法:

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

答案 1 :(得分:1)

问题(如上所述)是你的Reload()协程在你发射最后一个镜头之后和放弃MouseButton(0)之前在Update()中被调用了额外的时间。为了避免这种情况,我建议在开始协程之前检查以确保你没有重新加载:

 else if (ammoRemaining == 0 && !isReloading)
 {
     StartCoroutine(Reload());
 }
相关问题