Hi there, first time posting here. I know theres a lot of questions here about doors, but I need some very specific help (and quite speedily too).
Im using the fps controller prefab and want to have doors open when i walk into a trigger cube (or dummy object). I have my door mesh with open, close and idle animations, and also this dummy object set up. My question is this, What would be an effective script that will allow my player to walk into this trigger object, open the door and then close a set period of time later?
you might want to use a booean property and make it static and use the ontriggerenter function in a sript on the switch and make it change the static propertieto true. and then use the delay on that to change it back to false and on another script attached to the door make an if statement like if the static variable is true then play animation and then yield five seconds to play the closed animation
ok it's not easy without iTween. Download iTween and do some work. make it something like an OnTriggerEnter function with a line of iTween code telling it to move downwards and then an OnTriggerExit function and an iTween line telling it to move back up. very simple, but as I do not own anything from unity prefabs, I cannot do it my self but i've seen some tutorial videos so I understand iTween.
This is a simple script I made for Opening closing a door with an idle as initial. To make this work you will need to make the animations yourself and then attach this script to the animated GameObject… I made a door gameobject that just had two doors that would slide out when you pressed a button… The Activatable Script needs to be on the thing you want to use as an Activatable Object… like a button.
Attach this to the Object that Activates the Door
using UnityEngine;
using System.Collections;
public class Activatable : MonoBehaviour {
public DoorFunction door;
bool triggered = false;
bool mouseOver = false;
public bool Trigger {
get{return triggered;}
set{triggered = value;}
}
void Update()
{
if(triggered && mouseOver) //My triggered was sent from a button press of F in another script
sendTrigger(); //It just sets Trigger = true;
}
void OnMouseEnter()
{
mouseOver = true;
}
void sendTrigger()
{
door.Trigger = true;
triggered = false;
}
}
Attach this to the Door with Animations that will be Opened
using UnityEngine;
using System.Collections;
public class DoorFunction : MonoBehaviour {
public enum DoorState { open, closed, idle};
DoorState currentState = DoorState.idle;
public bool eventTriggered = false;
public bool Trigger {
get{return eventTriggered;}
set{eventTriggered = value;}
}
void Start()
{
StartCoroutine("CoStart");
}
IEnumerator CoStart()
{
while (true)
yield return StartCoroutine("CoUpdate");
}
IEnumerator CoUpdate()
{
yield return StartCoroutine("changeStates");
}
IEnumerator changeStates()
{
if(eventTriggered)
{
if(currentState == DoorState.idle)
{
currentState = DoorState.open;
}
else if(currentState == DoorState.open)
{
currentState = DoorState.closed;
}
else
currentState = DoorState.open;
switch(currentState)
{
case DoorState.idle :
animation.CrossFade("idle");
break;
case DoorState.closed :
animation.CrossFade("DoorClose");
yield return new WaitForSeconds(animation.GetClip("DoorClose").length);
animation.CrossFade("idle");
break;
case DoorState.open :
animation.CrossFade("DoorSliding");
yield return new WaitForSeconds(animation.GetClip("DoorSliding").length);
break;
}
}
eventTriggered = false;
yield return null;
}
}
This uses Coroutines to wait for the length of the animations to be finished before doing any other action.