Sliding Door Using SliderJoint2D - Unity

This will be a short and sweet little post on the usage of Slider Joint in Unity 2D. We will use this SliderJoint2D to create a simple Sliding Door which would slide open on pressing the Spacebar key of the keyboard, if it was in a closed state earlier, and vice versa.

Before we start making our sliding door, I'm sure you would like to know a bit about SliderJoint2D. If you have worked on the Box2D physics earlier, then the word Prismatic Joint might sound familiar to you. The prismatic joint there in Box2d is our SliderJoint2D in Unity.

SliderJoint2D, as the name suggests is useful when you want to add a linear movement between two bodies, where one of them would be fixed and the other moving. The moving part is generally called the slider and the body to which this slider is connected to is called the connected body.

If you would want to setup a scene just like I am doing here follow the steps below or you are just here to read how this SliderJoint2D is used so that you can use it in your game to create a sliding door or for some other thing, you can simply read on.

1. Create a New project in 2D mode, add a New Scene and name it anything you like to name it as, I'm going to name it as Sliding Door.

2. Download the sprites from the rar file here and add them to your project folder.
NOTE: Make sure the images are imported as sprites and not as textures.

3. Drag and drop the Wall sprite onto the Hierarchy of the scene. This should create a new Object of the same name. Rename the object to LeftWall. In the Inspector, you would see a transform and a Sprite Renderer component for this newly created object. Position this Wall at (-8, 0, 0). Set the Scale to (3, 1.5, 1).

4. Duplicate the object above and name it as RightWall. Position this object at (8, 0, 0).

5. It is time add the door to our scene. How would a Sliding Door scene be complete without the door? :D That was pretty lame, but yeah... Drag and drop the door from the Sprites folder you downloaded and added under your project's Assets folder.

6. Position it at (0, 0, 0). Set the Scale of the door to (2, 1, 1).

7. Add a Rigidbody2D component to this door by clicking on Add Component button in the Inspector panel and simply search for Rigidbody2D.

8. After you successfully add the Rigidbody2D component it is time to add the SliderJoint2D component. Repeat the same procedure as above, but here you would search for SliderJoint2D component instead.

9. Position the Main Camera at (0, 0, -10).

10. Configure the SliderJoint2D as in the image below:

Slider Joint 2D Config - Unity
- We uncheck the Collide Connected checkbox as we do not want the door sprite to collide with the wall. The Connected Rigidbody field will be empty as a result.
- The Ring which you see when you double click on the Door object in the Hierarchy, indicates the Anchor point, while the filled circle indicates the Connected Anchor.
- If you configure the SliderJoint2D as above, you will notice that the Filled Circle resides inside the Ring and are present at the left end of the door.
- You should also notice that there are two vertical green lines at both ends of the door. These lines signify the range that our door slides through. The Translation Limits is the field which defines these green lines. Shifting the anchor point will shift the movement range. Play with it to get a understanding of how it works.
- Since we will be using the motor to slide the door, we have enabled it. We will be modifying the Motor Force via script. 

Anchor and Connected Anchor Points
11. It is now time to control this door via script. Create a new C# script named SlidingDoor. Attach this script to the Door object. Open the script and add the below code to it:

using UnityEngine;
using System.Collections;

public class SlidingDoor : MonoBehaviour {
 
 SliderJoint2D door;
 JointMotor2D doorMotor;
 //Sliding speed of the door. Can be changed via the inspector
 public int slideSpeed = 100;
 //flag to determine if the door is closed or open
 bool isDoorOpen = false;
 // Use this for initialization
 void Start () {
  //get the sliderJoint2D component attached to the door object
  door = gameObject.GetComponent<SliderJoint2D>();
  //we will use the jointMotor2D to add speed and later set it back to the SliderJoint2D's motor as setting the motor speed of the sliderJoint2D doesn't work.
  doorMotor = door.motor;
 }
 
 // Update is called once per frame
 void Update () {
  //check if the spacebar key is pressed
  if(Input.GetKeyDown(KeyCode.Space)){
   //check if the door is already open
   if(!isDoorOpen)
    //set the motor speed to +100 so that the door will slide open
    doorMotor.motorSpeed = slideSpeed;
   else
    //set the motor speed to -100 so that the door will close
    doorMotor.motorSpeed = -slideSpeed;
   //invert the isDoorOpen flag once the door is opened or closed
   isDoorOpen = !isDoorOpen;
  }
  //set the sliderJoint2D's motor speed
  door.motor = doorMotor;
 }
}

Return to unity and test the scene. You should have a sliding door as in the gif below

SliderJoint2D Unity
You can change the Slide Speed field of the SliderJoint Script in the Hierarchy to alter the speed.

See you around.
Share on Google+

About Sujit Horakeri

Sujit Horakeri is a game freak just like any other next door guy you would come across. He is a Web Developer by Profession, Game Developer by Choice.
Connect with him on:
    Blogger
    Facebook

6 comments:

  1. i did everything here and still i dont get the door to slide

    ReplyDelete
    Replies
    1. Same here, doesn't work.
      Here is my code:

      SliderJoint2D door;
      JointMotor2D doorMotor;
      public int slideSpeed = 100;
      bool isDoorOpen = false;

      void Start()
      {
      door = gameObject.GetComponent();
      doorMotor = door.motor;
      }

      // Update is called once per frame
      void Update()
      {

      if (Input.GetKeyDown(KeyCode.Space))
      {
      if (!isDoorOpen)
      doorMotor.motorSpeed = slideSpeed;
      else
      doorMotor.motorSpeed = -slideSpeed;
      isDoorOpen = !isDoorOpen;
      }
      door.motor = doorMotor;
      }


      Any ideas?

      Delete
    2. Problem Fixed!
      Is Kinematic was turned on :D

      Delete
  2. Mobdro app is one of the best free apps available right now for streaming videos on your smartphones. If you are tired to buffering videos due to low internet speed, then you can use this app and forget about buffering once and for all. To know more about how to install it on your android device, windows phone, pc, blackberry, just take a look here
    Mobdro for iOS

    ReplyDelete
  3. There are times when you need to turn the lock so what you had as the bottom screw would now be the top screw.lexus key replacement cost

    ReplyDelete