2017-11-04 1 views
0

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(); 
    } 
} 
+0

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?)? –

+0

Ja. Mein weißer Kreis füllt sich alle 3 Sekunden. –

+0

Ich muss es mit meiner TankFiring Methode verbinden, damit der Countdown startet, wenn ich neu lade –

Antwort

1

Für den Anfang ist es nicht so etwas wie UpdateReload das wäre "einmal pro Frame genannt", da dies nicht der Fall ist eine vorbestimmte Unity-Funktion, es ist nur eine Funktion, die Sie erstellt haben (Sie können darüber lesen here). Ein weiteres Problem ist, dass Sie diese Funktion nirgendwo sonst in Ihren Skripten aufgerufen haben. Und selbst wenn Sie das getan haben, muss Mathf.Clamp() in eine Update() Funktion platziert werden, so dass es seinen Wert jedes Bild aktualisieren kann.

Ich habe einige Änderungen an den Skripten vorgenommen, die Sie gepostet haben, aber ich habe es noch nicht getestet. probieren Sie es aus und lassen Sie mich wissen, wie es geht:

ReloadTimer.cs

public class ReloadTimer : MonoBehaviour 
{ 
    public static ReloadTimer Instance { set; get; } 

    public Image filled; 
    public Text text; 
    public float coolDownTime = 3; 

    public bool isCoolingDown = false; 

    void Awake() 
    { 
     Instance = this; 
    } 

    void Update() 
    { 
     if (isCoolingDown == true) 
     { 
      filled.fillAmount += 1.0f/coolDownTime * Time.deltaTime; 

      int percentageInt = Mathf.RoundToInt((filled.fillAmount/coolDownTime) * 10); 
      text.text = percentageInt.ToString(); 
     } 
    } 
} 

TankShootingT.cs

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; 
     ReloadTimer.Instance.isCoolingDown = true; 

     Debug.Log("Reloading..."); 

     yield return new WaitForSeconds(reloadTime); 

     currentAmmo = maxAmmo; 
     isReloading = false; 

     ReloadTimer.Instance.isCoolingDown = false; 
     ReloadTimer.Instance.filled.fillAmount = 0.0f; 
    } 



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

hoffe, das hilft ein wenig.

Verwandte Themen