How can I implement a script that once the character see’s the object, a sudden sound plays like in the game “Slender”. Once you see him, there is a sudden sound that plays.
The way my scene goes is when the player unlocks a door, there will be a bloody mattress in the other room and I want the sudden audio to scare the player as soon as he see’s the bloody mattress.
Here’s my script hat i’ve used but keep replaying sound each time I look at it. I only want it played once.
using UnityEngine;
using System.Collections;
public class Jumpscare : MonoBehaviour {
public GameObject popup;
RaycastHit raycast;
void Update () {
Physics.Raycast(transform.position,transform.forward, out raycast);
if(raycast.transform.gameObject == popup)
{
popup.audio.Play();
//Destroy (gameObject, 1);
}
}
}