Search Unity

Push-to-open Door making

Discussion in 'Physics' started by ddulshan, Dec 28, 2014.

  1. ddulshan

    ddulshan

    Joined:
    Mar 16, 2014
    Posts:
    190
    Hi,

    I'm trying to make a door that when player is pushing against it (Walking/Running) through the door it moves. I need to know how to make such a thing.

    I've tried "Hinge Joint" thing all but I cant seems to get it right. I mean the door doesnt move at all when pushing at it.

    Please help.
     
  2. ddulshan

    ddulshan

    Joined:
    Mar 16, 2014
    Posts:
    190
    C'mon guys. Help the little noob. :D
     
  3. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,697
    Here's one way -- give your player a Rigidbody and a collider (e.g., Capsule Collider), not a Character Controller, so it's part of the Physics simulation.

    Put a rigidbody and collider on the wall or door frame, wherever the door connects. Put a hinge joint between it and your door. Add a Rigidbody and collider (e.g., Box Collider) to the door. Make sure Is Kinematic is UNticked. You may need to tweak Mass and tick Use Gravity.

    ---

    Another way to do a door is to use a door open/close animation. (To close a door, play the open animation with speed == -1.) You can create animations straight in Unity using the Animation view. Add a trigger collider in front of the door. When the player enters the trigger, play the animation, and optionally a door-opening audio clip if you want. This way, you don't need to set up a physics simulation of the door and its joint.
     
    Last edited: Dec 31, 2014
  4. ddulshan

    ddulshan

    Joined:
    Mar 16, 2014
    Posts:
    190
    Hi, thanks for the reply. I think the Animation Open/Close system is bit disturbing. That's why I'm going with the Physics thing.

    And about it. Is it a must to add a Rigid body and a Collidor to the Door Frame and the Player controller? Because the player already got a Collidor (and a Rigid body too, as I remember).

    Anyway I'll try it and let ya know. Thanks.
     
  5. gamedevelopingboy

    gamedevelopingboy

    Joined:
    Jul 9, 2014
    Posts:
    8
  6. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,697
    The door and player need rigidbodies and colliders. You probably don't need to add a rigidbody to the frame/wall.
     
  7. WintermuteDigital

    WintermuteDigital

    Joined:
    Dec 4, 2014
    Posts:
    19
    Cute. Also, just search Youtube. There's a good tutorial that I can't find, but you'll probably have better luck.
     
  8. ddulshan

    ddulshan

    Joined:
    Mar 16, 2014
    Posts:
    190
    @TonyLi : When I add a Rigidbody the player starts floating and Uncontrollable when pressed forward key. What now?
     
  9. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,697
    It depends on how you're controlling the player, but I bet if you set the player's rigidbody to Is Kinematic, it should be okay. On the door, however, leave Is Kinematic unticked.
     
  10. ddulshan

    ddulshan

    Joined:
    Mar 16, 2014
    Posts:
    190
    I tried player with Kinetic on and off. With on, like I said before and when off player doesn't move at all.

    About the door. Is there anything I need to change in the settings of Hinge Joint. And how should the Anchor be?
     
  11. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,697
    Check out the 3DBuzz video that @gamedevelopingboy linked above. It should explain how to set up the Hinge Joint on the door.

    What script(s) are you using to control the player?
     
  12. ddulshan

    ddulshan

    Joined:
    Mar 16, 2014
    Posts:
    190
    Tbh. My connection is not good for watching videos. That's why I posted here.

    Please tell me everything step by step including setting the Hinge joint settings.

    Please.
     
  13. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,697
    I encourage you to read the manual page on Hinge Joint. You'll learn a lot more than just following step-by-step directions without understanding what they're doing. The second paragraph of the Details section talks specifically about using Hinge Joint to set up a door.

    Also, get your character working first.
     
  14. ddulshan

    ddulshan

    Joined:
    Mar 16, 2014
    Posts:
    190
    Ok, figured how the Hinge Joint work. So, umm... what's this Connected Anchor thing? And how to make the door rotate on the Axis when the player is pushing it?
     
  15. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,697
    The anchor specifies the axis where the door swings when the player pushes it.
     
  16. ddulshan

    ddulshan

    Joined:
    Mar 16, 2014
    Posts:
    190
    I mean the Connected Anchor. And how to set a rotating limit, I don't want the door rotating all around. And when it reaches the limit it come back to the original position.
     
  17. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,697
    I believe the Connected Anchor specifies where it connects to its parent. . I've always left Auto Configure Connected ticked, and it wasn't in previous versions of Unity that I used.

    To set the rotating limit, tick Use Limits and set them under Limits.

    Tick Spring and fiddle with the values to make the door spring back to its original position. Or use a Spring Joint.

    The Hinge tutorial will clear up a lot of this if you can get videos working.
     
  18. ddulshan

    ddulshan

    Joined:
    Mar 16, 2014
    Posts:
    190
    Can you suggest me the values I should enter in the Limits. I used 90 as the Max limit and 0 as Min and it didn't move at all.
     
  19. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,697
    First make sure the door moves at all. Untick Use Limits. You may need to adjust the mass of the door to make it light enough to move. Once this works, tick Use Limits again. Try a big range, like -180 to 180, to make sure it works, then gradually tweak it down.
     
  20. ddulshan

    ddulshan

    Joined:
    Mar 16, 2014
    Posts:
    190
    Ok! Thanks!!!
     
  21. DryTear

    DryTear

    Joined:
    Nov 30, 2012
    Posts:
    312
    You could raycast, and if its hits the door, then you could rotate it in the opposite direction. This way, the door doesnt need a rigidbody, and the player doesnt need to be part of the physics.
     
  22. ddulshan

    ddulshan

    Joined:
    Mar 16, 2014
    Posts:
    190
    I'm not very familiar with Raycast, can you explain it a bit, I like the Idea.
     
  23. DryTear

    DryTear

    Joined:
    Nov 30, 2012
    Posts:
    312
    Code (csharp):
    1.  
    2. void Update()
    3.     {
    4.         RaycastHit hit = new RaycastHit();
    5.         float maxDistance = 5f;
    6.         float doorRotateSpeed = 5f;
    7.         if (Physics.Raycast(Camera.main.transform.position, Camera.main.transform.forward, out hit, maxDistance))
    8.         {
    9.             if(hit.transform.CompareTag("DoorFront"))
    10.             {
    11.                 hit.transform.parent.Rotate(Vector3.up * Time.deltaTime * doorRotateSpeed);
    12.             }
    13.             if (hit.transform.CompareTag("DoorBack"))
    14.             {
    15.                 hit.transform.parent.Rotate(Vector3.up * Time.deltaTime * -doorRotateSpeed);
    16.             }
    17.         }
    18.     }
    19.  
    so heres the setup : DoorFront and DoorBack objects have a tag with the same name, are basically invisible colliders,
    and when the raycast hits either one, it will either rotate in a certain way.
    When it rotates the parent object (DoorObject), it will rotate the children in a centered way, so if you move all the children to the left or the right, so that the pivot isnt in the center, the door rotates how it should.

    DoorObject
    ---DoorVisual
    ---DoorFront
    ---DoorBack
     
    Last edited: Jan 10, 2015
  24. DryTear

    DryTear

    Joined:
    Nov 30, 2012
    Posts:
    312
    Also i think there needs to be a script on the door to have some rotation constraints, because then it keeps on rotation if you keep on looking at the door.
     
    Last edited: Jan 11, 2015
  25. WintermuteDigital

    WintermuteDigital

    Joined:
    Dec 4, 2014
    Posts:
    19
    Check out the physics tutorial on the learn tab for raycast, basically shooting (casting) a line (ray) in a straight line. If it hits x, do y.
     
  26. ddulshan

    ddulshan

    Joined:
    Mar 16, 2014
    Posts:
    190
    @DryTear : Hmm, I thinks I cannot use this system for mine. Because of certain things.

    One is the thing you said about rotating the Parent, the way I've Built the Objects in the Hierarchy is arranged, I mean like. There's a Parent "World (Unstatic)" And another Object called "Doors" is inside the "World()" and even inside the "Doors" its arranged to specified by locations, like inside "Doors" there's another according to location, something like "Kitchen" and inside it is the final Door object. Lol.

    And next this still uses Objects other than the Door (The invisible Colliders). Etc...

    @Jay2001 :I know how it works, I'm not familiar how to use it. You know the right syntax and stuff. xD
     
  27. KingLlama

    KingLlama

    Joined:
    Jul 18, 2015
    Posts:
    199
    It seems the collider moves but the material objected doesn't.
     
  28. RealPpTheBest

    RealPpTheBest

    Joined:
    Jan 27, 2019
    Posts:
    64
    Hi! I know that I'm very late.. but this might help someone else in the future.. I've created a video on this topic, I would consider to check it out.. :

    This video demonstrates how to make a Push-To-Open door using Hinge Joint.

    I would also recommend to check out the video by @gamedevelopingboy . It's a good tutorial and demonstration.

    You can also read the official Manual on Hinge Joint by Unity.

    Btw, I don't think so you're a noob anymore ;)
     
  29. LightMiner

    LightMiner

    Joined:
    Nov 2, 2013
    Posts:
    7
    I know this thread is old but I have encountered a problem.. I did everything in the video and it worked perfectly as expected.. However, If I set the door mass higher than the player's mass, the player pushes the door slowly and more dynamically (I prefer it that way) but the problem is that the door pushes the player when bouncing back to the center.. How can I make it so that I push the door slowly but without the door pushing me back ...
     
  30. SavageX127

    SavageX127

    Joined:
    Nov 17, 2021
    Posts:
    1
    anyone know how to attach door knob and slider to door?

    upload_2021-12-7_20-5-40.png upload_2021-12-7_20-6-0.png
     
  31. Karantiin

    Karantiin

    Joined:
    May 2, 2014
    Posts:
    1