我有一个枚举器,用于显示你开枪时的效果。这是:
private IEnumerator ShotEffect()
{
gunAudio.Play();
laserLine.enabled = true;
yield return shotDuration;
laserLine.enabled = false;
}
当我拿出gunAudio.Play()
来时,代码工作正常。但是,当我添加它时,声音不会播放并且线路不会禁用。我认为有问题shotDuration
吗?
该变量gunAudio
在开始时被赋予音频组件,并且shotDuration
等待几秒钟。
编辑:
另外,如果我将它设置为在唤醒时播放,它会播放。因此,我认为代码存在问题,而不是组件。
这里是我调用枚举器的地方:
if (Input.GetKey(KeyCode.Mouse0) && Time.time > nextFire)
{
torsoDamage = (int)Random.Range(50f, 70f);
legDamage = (int)Random.Range(20f, 30f);
handDamage = (int)Random.Range(5f, 15f);
nextFire = Time.time + fireRate;
StartCoroutine (ShotEffect());
Vector3 rayOrigin = fpsCam.ViewportToWorldPoint (new Vector3(0.5f, 0.5f, 0.0f));
RaycastHit hit;
laserLine.SetPosition (0, gunEnd.position);
if (Physics.Raycast (rayOrigin, fpsCam.transform.forward, out hit, weaponRange))
{
laserLine.SetPosition (1, hit.point);
if (enemyAi.enemy_currentHealth != null)
{
if (hit.collider.gameObject.tag == "head")
{
enemyAi.Damage(100);
}
if (hit.collider.gameObject.tag == "torso")
{
enemyAi.Damage(torsoDamage);
}
if (hit.collider.gameObject.tag == "leg" || hit.collider.gameObject.tag == "arm")
{
enemyAi.Damage(legDamage);
}
if (hit.collider.gameObject.tag == "hand" || hit.collider.gameObject.tag == "foot")
{
enemyAi.Damage(handDamage);
}
}
if (hit.rigidbody != null)
{
hit.rigidbody.AddForce (-hit.normal * hitForce);
}
}
else
{
laserLine.SetPosition (1, gunEnd.position + (fpsCam.transform.forward * weaponRange));
}
}