Making a moveable door

As of version 1.38, Adventure Creator is capable of physics-based puzzles and interactions. In this tutorial, we'll create a door that can be manipulated by moving the mouse, like those seen in the Amnesia series by Frictional Games.

This tutorial assumes we're building a First Person game. If you need help creating a Player prefab capable of First Person, see this tutorial (or this one for UFPS integration).

Start by creating a scene that has a space for a door, that the player can fit through. The simple scene shown above was made with simple cubes.

Go to the Scene Manager and find the list of Moveable prefabs. Our door will be able to move, but only in a controlled manner, so create a new Draggable object by double-clicking the Draggable button.

Rename the Draggable object to Door. Reposition it so that it is in the lower corner of the frame, on the hinge's side.

The Draggable prefab comes with a Sphere Collider, which isn't necessary for our door, so remove the component by clicking Remove Component from the menu to the top-right of the Sphere Collider's component in the Inspector.

Next, we'll create a door mesh and add it as a child object. This tutorial will simply add a default Cube to the Door prefab, texture it, and scale it appropriately. Be sure that the mesh has an appropriately-sized Collider on the Default layer - it's this that will be the "click area" for the mouse cursor. In this case, we can simply add a Box Collider.

We'll now create the hinge that the door will pivot from. The Door will actually be re-positioned to wherever the hinge is, so first go to the Door and copy it's Transform component.

Back in the Scene Manager, double-click Hinge Track to create a new hinge. Rename the new object Door hinge. Your Hierarchy's _Moveable's folder should look like this:

From the menu to the top-right of the hinge's Transform component, click Paste Component Values to move it to the same position as the Door.

You will now see an white arc rotating around the object, with grey and white circles on each end. This represents the motion that the door will be capable of: the grey circle represents the "closed" position, while the white circle represents the "open" position.

We'll need to rotate it such that it will cause the door to pivot around the y-axis. Set the rotation values as (90, 180, 0). In the DragTrack_Hinge script Inspector, adjust the Radius value to better visualise the arc, and increase the Maximum angle to 120. This value affects how far the door can open.

Now we must constrain the door to only rotate about the hinge. In the Door GameObject's Moveable_Drag script Inspector, set the Drag mode to Locked To Track, and assign the Door hinge as the Track to stick to. Keeping the Initial distance along as zero will ensure the door is shut when the game begins.

Finally, look to the Rigidbody component. When we let go of the door, we want it to stop moving relatively quickly, so increase the Angular velocity to 10. The Mass value affects how easy the door is to move when we hold it: a value of 1 represents a 1:1 relationship between the door's movement, and the mouse movement. To give the door the effect of weight, increase the Mass to 4.

Save the scene and test the game. You'll notice that the door is rotated incorrectly when the scene begins:

This is because the Door has been rotated to meet the constraints of the hinge, and the door's mesh has been affected as well. Fortunately, since the mesh is a child object, we can correct it. Pause the scene, and re-position the door's mesh so that it fits in the door frame properly. Then, copy the Transform component as we did before. Stop running the scene, and paste the values back into the same component. The door will appear wrong again, but when we play the scene, it will be correct.

You should now be able to open and close the door by placing the cursor over it and dragging it in the direction it opens.

To give players a visual aid when dragging the door, you can display an icon from the Cursor Manager at the point the door is being held. In the Door's Moveable_Drag script Inspector, check Icon at contact point?, which will allow you to define which cursor is displayed.

Adventure Creator and this website are copyright Chris Burton, ICEBOX Studios

Go to top
Do NOT follow this link or you will be banned from the site!