Sound Based on Position

Hello,

I would like for an audio clip to play ONLY if the user/player inputs a key.

For example, if my character walked up to a radio and hit “Spacebar”, the radio would play an audio clip.

Does anyone know of a way I can accomplish this? Any help is greatly appreciated. Thank you!

–Velk

If you want the radio to play when the player presses the key anywhere near it, you can use DistanceToObject (I think that’s what it is anyways). If you want it only to play when the player is in front of it and presses a button you can use raycasts or trigger areas. Here’s an example of using Triggers:

var display = "Sign Text Here";
var signpost : Transform;
var Player : GameObject;

function OnTriggerStay (other : Collider) {
	if (other.name == "Player") {
		if (Input.GetButtonDown("Action")  Player.GetComponent("Player").reading == false) {
			Player.GetComponent("Player").TextBox(display);
			other.transform.LookAt (signpost);
			other.transform.rotation.x = 0;
			other.transform.rotation.z = 0;
		}
	}
}

This code is what I use for reading signs in one of my games. It is attached to a trigger, which is childed to the sign mesh (which I assign in the editor). What it does is, if it detects the player inside of the trigger and the player presses the action button, it makes the player look towards the sign (in case they’re facing away from it, which doesn’t make much sense if they’re supposed to be reading it), and then calls the TextBox function in the player’s script. Yours would be a Radio function, and would have some code to start playing the sound.