@Krahazik
Best way to go about this (at least for me) was to create an empty object called “Sliding Door” and add functionality to it. I would put the meshes (geometry, models, take your pick) as children of the empty object called “Sliding Door”.
Then I create a script called “SlidingDoorController” on it, and add an Animator Component. I create a new Animation Controller called “Sliding Door Controller”, and drag it into “Sliding Door’s” animator component.
I then create an empty child of “Sliding Door” and call it “Door Trigger”. I add the BoxCollider Component to it. I position it in the middle of the doors and resize the BoxCollider to extend to either side of the door a certain distance (I found 2 works fine for a normal sized door). Make sure to also check the “Is Trigger” checkbox here. I add a component to the “Door Trigger” object just called “DoorTrigger”.
That is all of the setup for the scripting done, now onto the real meat of this!
First let’s make it so that we can see if a player has gotten close enough to the door to open it. We do that in “DoorTrigger”.
public class DoorTrigger : MonoBehaviour {
public enum DoorEvents
{
None,
PlayerDetected,
};
private DoorEvents events = DoorEvents.None;
public void OnTriggerEnter(Collider other)
{
if(other.tag == "Player")
{
events = DoorEvents.PlayerDetected;
}
}
public void OnTriggerExit(Collider other)
{
if (other.tag == "Player")
{
events = DoorEvents.None;
}
}
public DoorEvents Events
{
get { return events; }
}
}
As a caveat what ever you use as your player in this particular case MUST have the “Player” tag for the events to work correctly. Now “DoorTrigger” is done.
Next we go back to “SlidingDoorController” to finish off the scripting.
public class SlidingDoorController : MonoBehaviour {
public List<DoorTrigger> doorTriggers;
private Animator doorAnimator;
private bool openDoor = false;
// Use this for initialization
void Start () {
doorAnimator = GetComponent<Animator>();
}
// Update is called once per frame
void Update () {
foreach(DoorTrigger d in doorTriggers)
{
if(d.Events == DoorTrigger.DoorEvents.PlayerDetected)
{
if (!openDoor)
{
openDoor = true;
}
break;
}
else
{
if (openDoor)
{
openDoor = false;
}
}
}
doorAnimator.SetBool("openDoor", openDoor);
}
}
Back in the Inspector for the “Sliding Door” drag and drop the “Door Trigger” child object into the “Door Triggers” object.
Now you may get a warning or an error that there is not a boolean in the Animator component called “openDoor”, well we are going to fix that right now!
Select the Animation Controller we made earlier and open it in the Animator window (under Window → Animator). Add a parameter of type bool and call it “openDoor”. We are now going to set up our animation states while we are here. Go ahead and right-click in the grey grid area and select “Create State → Empty”. The first one we make will be our default state and will be colored yellow. Rename the state you just made “Idle” for now. Create another state and call it “Open” and create one more state called “Closing”. Change the “Speed” of the “Closing” State to -1.
Now that our states are setup we have to connect them. Select the “Idle” state and right-click it. Select “Make Transition” and click the “Open” state we made. Select the new arrow we created. Uncheck the “Has Exit Time” checkbox, and add a condition to it (press the “+” button in the conditions area). It should automatically add the “openDoor” boolean to the conditions. Create a transition from “Open” to “Closing”. Add a condition to it.
Note: Make sure you change “openDoor” to false in this transition.
Finally add one more transition from the “Closing” state to the “Idle” state.
Now we need to make the actual open animation. Since sliding doors open and close in the same manner we can just make one animation and play the opening animation in reverse to close the doors.
Tip: For this next step make sure you can see the sliding door in the “Scene” window.
So select our “Sliding Door” and open the “Animation” window (Window ->Animation). There should be a button in the animation window to create a new Animation clip. Click it and save the clip as “OpenSlidingDoor”.
Click the “Add Property” button and select the position the right door panel. Repeat this for the left door panel. Move the red line in the “Animation” window to the end of the clip (to the right). Select one of your door panels and move it to where its “open” position is. Repeat this for the other panel. Press the “Play” button in the “Animation” window to preview the animation.
Go back to the “Animator” window to see a new state has been added. Select our “Open” state and select the “Motion” field. Select our newly made “OpenSlidingDoor” animation clip. Repeat this for the “Closing” state.
To test this add a sphere to the scene. Change the sphere’s tag to “Player” or add it if it is not there. Add a “Rigidbody” component to the sphere and uncheck the “Use Gravity” check box, and check the “Is Kinematic” checkbox. Hit play in the editor and move the sphere in the Scene preview window toward the door to see if the door opens and closes.
From here you can expand functionality where the door is locked or broken.