I have a door that can be opened, and I have an animation that I want it to be played when I open the door and only happen once (like a jumpscare). I tried to use an empty object with box collider as a trigger but when the player walk into it, the animation played although I didn’t open the door. Here is the script I used to create door open/close in case I need to modify it:
#pragma strict
private var guiShow : boolean = false;
var isOpen : boolean = false;
var door : GameObject;
var rayLength = 10;
var customSkin : GUISkin;
var DoorOpenSound : AudioClip;
var DoorCloseSound : AudioClip;
function Update()
{
var hit : RaycastHit;
var fwd = transform.TransformDirection(Vector3.forward);
if(Physics.Raycast(transform.position, fwd, hit, rayLength))
{
if(hit.collider.gameObject.tag == "Door")
{
guiShow = true;
if(Input.GetKeyDown("e") && isOpen == false)
{
door.animation.Play("OpenDoor");
isOpen = true;
guiShow = false;
audio.PlayOneShot(DoorOpenSound);
}
else if(Input.GetKeyDown("e") && isOpen == true)
{
door.animation.Play("CloseDoor");
isOpen = false;
guiShow = false;
audio.PlayOneShot(DoorCloseSound);
}
}
}
else
{
guiShow = false;
}
}
function OnGUI()
{
if(guiShow == true && isOpen == false)
{
GUI.skin = customSkin;
GUI.Box(Rect(Screen.width / 2, Screen.height / 2, 100, 25), "E open");
}
if(guiShow == true && isOpen == true)
{
GUI.skin = customSkin;
GUI.Box(Rect(Screen.width / 2, Screen.height / 2, 100, 25), "E close");
}
}