【发布时间】: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);
}
}
}
}
【问题讨论】:
-
如果
source是被销毁的同一个游戏对象层次结构的一部分,那么是的,这可能是问题...