【问题标题】:Can not play disabled audio source (Unity 2d)无法播放禁用的音频源(Unity 2d)
【发布时间】:2018-12-18 20:17:23
【问题描述】:

我有一个小问题;我正在尝试开发自己的无尽跳跃游戏。这就是为什么我创建了一种可以发射小型射弹的敌人类型。每次射击时,都应该播放声音,但不会。调试:“无法播放禁用的音频源。”

最初这仅适用于预制件,但最近它也不适用于第一个原始对手。 (我有点搞砸了代码中的一些东西)

每个建议的解决方案都会对我有很大帮助:)

我知道,有一些关于这个主题的帖子,但这些都没有帮助......

有点乱的代码:

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

public class FlyingEnemy : MonoBehaviour {

public float startTimeBtwShots;
private float timeBtwShots;
private Vector3 spawnPoint;

public AudioSource shot;

public Animator animator;

private float flyingSpeed;
private float turningPoint;

private bool moving;

public GameObject projectile;

public GameObject player;

void Start () 
{
    turningPoint = Mathf.Abs(transform.position.x);
    flyingSpeed = 0.008f;
    timeBtwShots = startTimeBtwShots;
    moving = true;
    shot.enabled = true;
}

void Update () 
{
    //Ignore, moves enemy from left to right and back 
    if (moving)
    {
        transform.position = new Vector3(transform.position.x + flyingSpeed, transform.position.y, transform.position.z);
    }
    if (transform.position.x >= turningPoint || transform.position.x <= turningPoint * -1)
    {
        flyingSpeed *= -1;
        transform.position = new Vector3(transform.position.x + flyingSpeed, transform.position.y, transform.position.z);
        if (flyingSpeed <= 0) transform.eulerAngles = new Vector3(0, 180, 0);
        else if (flyingSpeed >= 0) transform.eulerAngles = new Vector3(0, 0, 0);
    }

    //Important part: projectile gets spawn and a bit earlier the sound should be played...
    if (timeBtwShots <= 0)
    {
        spawnPoint = transform.position;
        spawnPoint.y -= 0.35f; 
        Instantiate(projectile, spawnPoint, Quaternion.identity);
        timeBtwShots = startTimeBtwShots;
        animator.SetBool("Shooting", false);
    }
    else
    {
        timeBtwShots -= Time.deltaTime;
        if (timeBtwShots <= 0.3)
        {
            shot.volume = 1;
            shot.Play();
        }
        if (timeBtwShots <= 0.6)
        {
            animator.SetBool("Shooting", true);
        }
    }

    if (player.transform.position.y >= transform.position.y + 5.5f)
    {
        Destroy(gameObject);
    }
}
}

提前谢谢你!!

自我 |雅各布

【问题讨论】:

  • 如果您将Debug.Log("blah blah") 放入您调用if 的块中Play,是否会记录任何内容?
  • 是的,这行得通

标签: c# unity3d audio


【解决方案1】:

你应该试试你的 Start 函数:shot.gameObject.SetActive(true); 这可能是问题所在。

【讨论】:

  • 谢谢,但没有解决。
猜你喜欢
  • 2017-01-06
  • 2014-12-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-21
  • 2023-03-05
相关资源
最近更新 更多