So I have a narration based game like the stanley parable, I want to make so when the player touches the invisible wall I have setup that an audio plays. Also I want the audio to be a variable so I can change it for each area I place the wall. I have no idea how to achieve this, it would be great if I could get some help, thanks!
public var NarratorAudio = AudioClip;
if Collision.Player {
NarratorAudio.Play;
}
That is literally my entire script. It doesn’t work so, can I get some help?
well you’re almost there. However, in the way your script is right now, Unity doesn’t know what to do with your code - when should it be executed? Typically, you would wrap everything inside a Class (not sure if required in JavaScript) and the code you’ve written should go into the method that is called when the player collides with the wall. The method you’ll want to implement is most likely OnCollisionEnter.
Be aware that “OnCollisionEnter” is called for ANY collision, not just with the player. So you will want to check if the object you’ve collided with actually is the player (and not any other object flying around in your scene). You can do that fairly quickly by checking if the Tag of the other game object is the player tag. Of course, for this to work you must assign the “player” tag to the player game object in your scene. And don’t forget to add a suitable Collider to your player and your invisible wall (e.g. box collider for the wall, capsule collider for the player), otherwise the collision will not be detected and your function will not be called!
I’m not posting the entire solution because I rarely ever use JavaScript in Unity, so it is likely to be full of syntax errors anyways, but you should be able to manage it with the two links I’ve given above