首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >重新加载声音没有在正确的时间播放

重新加载声音没有在正确的时间播放
EN

Stack Overflow用户
提问于 2017-05-23 18:28:37
回答 1查看 49关注 0票数 0

在这个重新加载脚本中,枪重新加载,然后播放重装声音。问题是,重新加载应该首先播放。另一个问题是,我想摆脱周围的工作,我做的重新加载的关键工作。当我按下“重装”键时,他们之所以让Ammo =0,是因为在我添加弹药时,弹药不会重置为8,我只是不明白为什么当我重新加载声音时,直到延迟结束后才会播放。谢谢你抽出时间阅读这篇文章。

代码语言:javascript
复制
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;
        }
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-05-23 19:06:03

发生这种情况的直接原因是,对于正在重新加载的每一个帧,都会调用reloadfx.Play ();。这意味着它一遍又一遍地重新启动,直到你的重新加载完成,它才能完整地播放。

但是,造成这种情况的根本原因是如何围绕重新加载逻辑构建条件。我的建议是,只要有一个标志,实际上表明你的枪是重新装填,而不是间接信号通过Ammo == 0。当Input.GetButtonDown ("Reload")发生且重新加载的条件正确时,播放重新加载声音并将标志设置为true。然后,在重新加载完成后,将标志设置为false。

有许多方法可以改变这种情况,但我觉得对您的方法做一个非常简单的重写:

代码语言:javascript
复制
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;
}

希望这能有所帮助!如果你有任何问题,请告诉我。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44142435

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档