在此重新加载脚本中枪重新加载,然后播放重载声音。问题是重新加载应该首先发挥。另一个问题是我想摆脱我做的工作,使重装密钥工作。他们之所以我按下重装是因为我将Ammo设为0是因为在我添加弹药之前我不会重新设置为8.我只是不明白为什么当我重新加载声音时没有#n;'直到延迟结束后才玩。感谢您抽出宝贵时间阅读这篇文章。
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class Reload : MonoBehaviour
{
//amount of bullets in the mag
public static int Ammo;
private float timer = 0.0f;
public float reloadTime = 3.0f;
//calls upon a command in a diffrent script that cancels the ablity to shoot
public PauseManager reload;
//plays the audio
public AudioSource reloadfx;
Text text;
void Awake ()
{
reloadfx = GetComponent <AudioSource> ();
text = GetComponent <Text> ();
Ammo = 8;
}
void Update ()
{
//I used this line as a underhanded way of allowing the Reload key to reload the pistol.\
//This is also my work around.
if (Input.GetButtonDown ("Reload") && Ammo < 8)
{
Ammo = 0;
}
//IF Ammo is 0 or I press the reload and i am not full of ammo then reload
if (Ammo == 0 || Input.GetButtonDown ("Reload") && Ammo < 8)
{
//plays the sound
reloadfx.Play ();
ReloadAction ();
}
else
{
//enable the ablity to shoot
reload.shoot = true;
}
//display the ammunition
text.text = Ammo + "/8";
}
public void ReloadAction()
{
//for as long as the timer is smaller then the reload time
if (timer < reloadTime)
{
//disable the abillity to shoot
reload.shoot = false;
//count the time
timer += Time.deltaTime;
}
else
{
//after the reload reset the timer and ammo count
Ammo = 8;
timer = 0.0f;
}
}
}
答案 0 :(得分:0)
发生这种情况的直接原因是因为正在为重新加载的每一帧调用reloadfx.Play ();
。这意味着它会一遍又一遍地重新启动,直到最后你的重新加载完成并且它可以完整地播放。
然而,其根本原因在于您如何构建重新加载逻辑的条件。我的建议是只有一个标志,实际上表明你的枪正在重新加载,而不是通过Ammo == 0
间接发出信号。当Input.GetButtonDown ("Reload")
出现并且条件正确重新加载时,播放重新加载声音并将标志设置为true。然后,在重新加载完成后将标志设置为false。
有很多方法可以改变这种情况,但我觉得这是一种非常简单的改写方法:
private bool isReloading = false;
void Update ()
{
if (!isReloading)
{
// When ammo has depleted or player forces a reload
if (Ammo == 0 || (Input.GetButtonDown ("Reload") && Ammo < 8))
{
// Play sound once, signal reloading to begin, and disable shooting
reloadfx.Play ();
isReloading = true;
reload.shoot = false;
}
}
else
{
// isReloading will be the opposite of whether the reload is finished
isReloading = !ReloadAction();
if (!isReloading)
{
// Once reloading is done, enable shooting
reload.shoot = true;
}
}
text.text = Ammo + "/8";
}
// Performs reload action, then returns whether reloading is complete
public bool ReloadAction()
{
//for as long as the timer is smaller then the reload time
if (timer < reloadTime)
{
timer += Time.deltaTime;
}
else
{
//after the reload reset the timer and ammo count
Ammo = 8;
timer = 0.0f;
return true;
}
return false;
}
希望这有帮助!如果您有任何问题,请告诉我。