- Home /
How to make a double sliding door?
Ok so I have been perusing through the manual, tutorials, and previous sliding door answers and so far I have not found anything applicable. I am trying to build a sci-fi style double sliding door. I am new to unity and to C# scripting and unity's variation of it. Using C# as the primary scripting language for my scripts. A door frame assembly with 2 door objects that open and close in oposite directions when the assembly is interacted with.
I have built a simple physical camera drone for moving about my scene and testing.
Using information from one previous answer I have a door object attached to the frame assembly using a custom joint configures to act like a slide track. The door will slide using physics along its track when I bump into it with my camera drone. The door object is one of 2 that will be in the assembly. Only have 1 setup for testing purposes so far.
The door object is a child object of the door frame. I have a pair of box colliders configured on the root object as interactable defining an interaction area along the side edges of the door frame.
I do have a pair of animations created, one for opening the door, one for closing the door.
At this point I am stuck. I have a generic script created and attached to the root of the entire door assembly object. I am unable to figure out how to identify the child door object or to trigger movement when the interaction zones of the assembly are clicked on. Also not sure if I should use animations to run the doors, or use physics by imparting a force to the door to move it from open to close and vice versa.
Eventually I do plan on having a mechanic where a door panel can be broken or removed as well as for dissabling the door mechanisim so that it has to be manually manipulated by the player. Not sure if that matter now or not for determing the best method for moving the door objects for open/close?
Why are you using physics for this? A simple animation would give the same effect, and you would not have to worry about the physics engine causing issues.
Answer by Senuska · Aug 29, 2017 at 07:39 PM
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.
Think I might have missed something as the door is not responding. Can not tell if anything is happening.
Added some print commands to the DoorTrigger script, and it looks like it is detecting and reacting to my player object. It appears the animation is not being triggered.