how would i make a GameObject that, when you press a certain key up close to it, plays an audio clip, kinda like those radios in cod zombies??
This is really simple. Here’s some C#. Make all your radios have the tag “Radio”. This is the simplest case- you might want to only test for things in front of the player, instead of everything in range. Make sure that every ‘radio’ tagged object has an audio component (obviously). Change the ‘1’ after the Vector3.Distance bit to a public variable if you want to tweak how far off you can activate them.
if(Input.GetButtonDown("PlayRadio"))
{
GameObject[] radios = GameObject.GetGameObjectsWithTag("Radio");
foreach(GameObject radio in radios)
{
if(Vector3.Distance(transform.position, radio.transform.position) < 1)
{
// We are close to a radio!
radio.audio.Play();
}
}
}
Thats C# script @sxg5 ...
if(Input.GetButtonDown("PlayRadio")){
var radios : GameObject[] = GameObject.FindGameObjectsWithTag("Radio");
for(var radio : GameObject in radios){
if(Vector3.Distance(transform.position, radio.transform.position) < 1){
//We're close to a radio!
radio.audio.Play();
}
}
}
This is JavaScript/UnityScript with the “var” things, or whatever they are lol…
if this works for you, mark @syclamoth 's answer as the accepted one.