【问题标题】:Sound not playing on last hit最后一击没有播放声音
【发布时间】:2021-11-23 22:30:50
【问题描述】:

我有一个问题,我的受伤声音在最后一击时没有播放。在其他命中它工作得很好。我认为这是因为游戏对象被破坏了。我已经通过将脚本放在我的项目符号上并更改标签来尝试过。 Debug.Log 在最后一次命中时也能完美运行。我希望有人可以帮助我。

无论如何,这是我的代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class HealthSystem : MonoBehaviour
{
    
    public AudioSource source;
    
    public AudioClip clip;
    
    public Image healthBar;

    public float healthAmount = 100;

    public GameObject bullet;
    

    void Start()
    {

    }
    private void Update()
    {
        if(healthAmount <= 0)
        {
            Destroy(gameObject);
        }
    }

    public void TakeDamage(float Damage)
    {
        source.PlayOneShot(clip);
        healthAmount -= Damage;
        healthBar.fillAmount = healthAmount / 100;
    }

    public void Healing(float healPoints)
    {
        healthAmount += healPoints;
        healthAmount = Mathf.Clamp(healthAmount, 0, 100);

        healthBar.fillAmount = healthAmount / 100;
    }
    
    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.CompareTag("Bullet"))
        {
            if (!source.isPlaying)
            {
                Debug.Log("i have are played");
                TakeDamage(20);
            }
        }
    }
}

【问题讨论】:

标签: c# unity3d


【解决方案1】:

正如你所说:因为你的游戏对象被最后一颗子弹摧毁了,AudioSource 也被摧毁了,它无法播放完整的声音。解决方法是什么?有 2 个,您可以:

  • 生成新的GameObject 只是为了播放声音并在之后立即销毁它
  • 隐藏/禁用对象(就像它被销毁一样),播放声音,一旦播放样本,就销毁它

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多