gpt4 book ai didi

c# - 无法播放禁用的音频源(Unity 2d)

转载 作者:行者123 更新时间:2023-12-02 23:30:47 27 4
gpt4 key购买 nike

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

最初,这种方法在预制件上是行不通的,但最近在第一个原始对手上也行不通。 (我有点弄乱了代码中的内容)

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

我知道,那里有一些有关此主题的帖子,但是这些都没有帮助...

有点凌乱的代码:

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);
}
}
}

先感谢您!!

自我雅各布

最佳答案

您应该尝试使用Start函数:shot.gameObject.SetActive(true);
可能是问题所在。

关于c# - 无法播放禁用的音频源(Unity 2d),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53840417/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com