2017-11-04 5 views
0

私は3秒ごとにカウントダウンするタイマーを持っています(白丸)。 ReloadTimerという名前のスクリプトが添付されています。Unity:リロード中に武器のリロードカウントダウンを開始するにはどうしたらいいですか?

私は弾丸(TankShooter)を発射し、3秒間リロードするスクリプトを持っています。

リロード中にカウントダウンを開始するにはどうすればよいですか?

私は多くのユニティフォーラムを見てみましたが、アドバイスはうまくいかなかった。手始めに

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

問題が解決しないのですか? 3秒間の撮影後にクールダウンするようなGUI要素(白丸)がありますか? –

+0

はい。私の白丸は3秒ごとにいっぱいです。 –

+0

私はTankFiringメソッドにリンクして、リロード中にカウントダウンを開始する必要があります –

答えて

1

これがないと、 "フレームごとに一度と呼ばれる" ことになるUpdateReloadのようなものはありませんあなたが作成した関数です(これについてはhereを読むことができます)。もう1つの問題は、スクリプトのどこにでもその関数を呼び出さないことです。たとえそうでも、Mathf.Clamp()Update()関数に配置する必要があります。そのため、各フレームの値を更新できます。

投稿したスクリプトにいくつか変更を加えましたが、まだテストしていません。それを試してみると、私はそれが行く方法を知ってみましょう:

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

これは少しお役に立てば幸いです。

関連する問題