Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 统一:当我重新装弹时,如何开始武器装弹倒计时?_C#_Unity3d_Unity5 - Fatal编程技术网

C# 统一:当我重新装弹时,如何开始武器装弹倒计时?

C# 统一:当我重新装弹时,如何开始武器装弹倒计时?,c#,unity3d,unity5,C#,Unity3d,Unity5,我有一个计时器,每3秒倒计时一次白色圆圈。它附带了一个名为ReloadTimer的脚本 我有一个脚本,可以发射子弹,坦克射手,然后重新加载3秒钟 如何使我的倒计时在重新加载时开始 我试着看了很多Unity论坛,但这些建议都不起作用 重新加载计时器.cs 坦克射手 对于初学者来说,没有像UpdateLoad这样的东西会在每帧调用一次,因为这不是一个预先确定的Unity函数,它只是一个您创建的函数,您可以阅读它。另一个问题是,您甚至没有在脚本中的任何其他地方调用该函数。即使您这样做了,Mathf.C

我有一个计时器,每3秒倒计时一次白色圆圈。它附带了一个名为ReloadTimer的脚本

我有一个脚本,可以发射子弹,坦克射手,然后重新加载3秒钟

如何使我的倒计时在重新加载时开始

我试着看了很多Unity论坛,但这些建议都不起作用

重新加载计时器.cs

坦克射手


对于初学者来说,没有像UpdateLoad这样的东西会在每帧调用一次,因为这不是一个预先确定的Unity函数,它只是一个您创建的函数,您可以阅读它。另一个问题是,您甚至没有在脚本中的任何其他地方调用该函数。即使您这样做了,Mathf.Clamp也需要放在一个更新函数中,这样它就可以在每一帧更新它的值

我对你发布的脚本做了一些修改,但是我还没有测试它们。试试看,让我知道进展如何:

重新加载计时器.cs

坦克射击


希望这有点帮助。

我不明白问题出在哪里?你有没有像GUI元素一样的东西——拍摄3秒后填充的白色圆圈,比如冷却时间??是的。我的白色圆圈每3秒就填满一次。我需要把它和我的坦克射击方法联系起来,这样当我重新装弹时,倒计时就开始了
[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();
}
}
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();
    }
}
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();
        }
    }
}
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();
    }
}