Ich habe einen Timer, der alle 3 Sekunden herunter zählt (Der weiße Kreis). Es hat ein Skript namens ReloadTimer angehängt.Einheit: Wie starte ich meinen Countdown zum Nachladen der Waffe, wenn ich neu lade?
Ich habe ein Skript, das Kugeln (TankShooter) feuert und für 3 Sekunden neu lädt.
Wie kann ich den Countdown starten, wenn ich neu lade?
Ich habe versucht, viele Unity-Foren zu sehen und der Rat hat nicht funktioniert.
ReloadTimer.cs
[ExecuteInEditMode]
public class ReloadTimer : MonoBehaviour
{
public Image filled;
public Text text;
public float maxValue = 3;
public float value = 0;
// UpdateReload is called once per frame
public void UpdateReload()
{
value = Mathf.Clamp(value, 0, maxValue);
float amount = value/maxValue;
filled.fillAmount = amount;
text.text = value.ToString();
}
}
TankShooter
public int m_PlayerNumber = 1;
public Rigidbody m_Shell;
public Transform m_FireTransform;
public AudioSource m_ShootingAudio;
public AudioClip m_FireClip;
public float m_ShellVelocity = 100f;
private string m_FireButton;
public int maxAmmo = 5;
private int currentAmmo;
public float reloadTime = 2f;
private bool isReloading = false;
public ReloadTimer reloadTimer;
public class TankShootingT : NetworkBehaviour
{
public ReloadTimer reloadTimer;
private void Start()
{
if (!isLocalPlayer)
{
return;
}
currentAmmo = maxAmmo;
m_FireButton = "Fire" + m_PlayerNumber;
}
private void Update()
{
if (isReloading)
return;
if (currentAmmo <= 0)
{
StartCoroutine(Reload());
return;
}
reloadTimer.UpdateReload();
if (m_FireButton == "Fire1" && Input.GetButtonUp(m_FireButton))
{
// we released the button, have not fired yet
CmdShoot();
}
}
IEnumerator Reload()
{
isReloading = true;
Debug.Log("Reloading...");
yield return new WaitForSeconds(reloadTime);
currentAmmo = maxAmmo;
isReloading = false;
}
[Command]
private void CmdShoot()
{
currentAmmo--;
// Instantiate and launch the shell.
Rigidbody shellInstance = Instantiate(m_Shell, m_FireTransform.position, m_FireTransform.rotation) as Rigidbody;
shellInstance.velocity = m_ShellVelocity * m_FireTransform.forward;
// Server spawns the shell
NetworkServer.Spawn(shellInstance.gameObject);
m_ShootingAudio.clip = m_FireClip;
m_ShootingAudio.Play();
}
}
Ich verstehe nicht, was das Problem ist? Haben Sie etwas wie ein GUI-Element (der weiße Kreis), das sich nach 3 Sekunden der Aufnahme füllt (so etwas wie eine Abkühlzeit?)? –
Ja. Mein weißer Kreis füllt sich alle 3 Sekunden. –
Ich muss es mit meiner TankFiring Methode verbinden, damit der Countdown startet, wenn ich neu lade –