我在声音触发方面遇到了麻烦。下面的脚本附加到带有AudioSource组件和声音剪辑的硬币预制对象。一切都在工作,除了没有声音。我做错了什么?
<div/>
答案 0 :(得分:0)
可能不是你想要的,但我通常会实例化这样的声音:(我目前无法访问Unity,所以裸露了)
using UnityEngine;
using System.Collections;
public class CoinPickup : MonoBehaviour
{
public AudioSource coinSound; //set in inspector
void OnTriggerEnter(Collider other) // other is a reference to the other trigger collider we have touched
{
if (other.gameObject.tag == "Player") //or you could do other.gameObject.name == "Player"
{
coinSound.Play(); //play the coin sound
Destroy(gameobject); // destroy our coin, it has been picked up!
}
}
}
我不确定,但我不认为你可以在去激活的游戏对象上播放声音。
这种方式应该在玩家接触到你的硬币时播放声音,此时它会播放你的声音,然后硬币会自行消失。
希望这会有所帮助:)
答案 1 :(得分:0)
切换脚本。因此,当硬币击中玩家时,不要触发,以便当玩家击中硬币然后将该脚本附加到玩家时。
另外,将其设为AudioClip
并向AudioSource
Player
public AudioClip coinSound; //set in inspector
void OnTriggerEnter(Collider other) // other is a reference to the other trigger collider we have touched
{
if (other.gameObject.tag == "Coin")
{
coinSound.Play(); //play the coin sound
}
}