Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Opening and closing doors

Discussion in 'Animation' started by KidMakesGames, Aug 16, 2015.

  1. KidMakesGames

    KidMakesGames

    Joined:
    Aug 12, 2015
    Posts:
    19
    I can't find a tutorial that can learn me how to open and close doors when I press F(or any other key).I REALLY NEED HELP.A


    I just someone to tell me or link me a tutorial on how to close and open doors.
     
  2. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,533
    Try the PlayMaker tutorials. You'll need to buy a copy of PlayMaker, but it's probably the easiest way to get started with all this stuff.
     
    KidMakesGames likes this.
  3. KidMakesGames

    KidMakesGames

    Joined:
    Aug 12, 2015
    Posts:
    19
    Thank you!
    :)
     
  4. AaronC

    AaronC

    Joined:
    Mar 6, 2006
    Posts:
    3,552
    Some examples for you to try

    If door open/close is keyframed: (And the two animations are named "Open" and "Close")
    Code (csharp):
    1.  
    2. //the object with the animation on it
    3. var Door: GameObject;
    4.  
    5. function OnTriggerEnter(){
    6. Door.animation.Play("Open");
    7. }
    8. function OnTriggerExit(){
    9. Door.animation.Play("Close");
    10. }
    11.  
    ^ as simple as that is, it can get broken if you trigger one animation before the other is finished.
    Heres an example of a sliding door, or gate, that avoids that:
    Code (csharp):
    1.  
    2.  
    3. var gate: Transform;
    4. var gateOpenPosition : float = 2.0;
    5. var gateClosedPosition : float = 0.0;
    6. var gateSpeed : float = 1.0;
    7.  
    8. var open : boolean = false;
    9.  
    10. function Start(){
    11.    collider.isTrigger=true;
    12.    renderer.enabled=false;
    13.  
    14. }
    15.  
    16. function OnTriggerEnter(other: Collider){
    17.  
    18.    if(other.CompareTag("Player")||other.transform.name==("First Person Controller"))
    19.    {
    20.      open = true;
    21.    }
    22. }
    23.  
    24. function OnTriggerExit(other: Collider){
    25.  
    26.    if(other.CompareTag("Player")||other.transform.name==("First Person Controller"))
    27.    {
    28.      open = false;
    29.  
    30.    }
    31. }
    32.  
    33. function Update() {
    34.  
    35.  
    36.      if(open)
    37.      gate.position.x = Mathf.MoveTowards(gate.position.x, gateOpenPosition, gateSpeed * Time.deltaTime);
    38.  
    39.      else
    40.      gate.position.x = Mathf.MoveTowards(gate.position.x, gateClosedPosition, gateSpeed * Time.deltaTime);
    41.    
    42.  
    43.  
    44. }
    45.  
    This final one doesnt need animations either, you have to set the open position yourself (Based on y rotation in the inspector). This is a rotation door open.

    Make sure your door objects "center" or pivot point is where a hinge would naturally be. THis only opens once.

    Code (csharp):
    1.  
    2. var door: Transform;
    3. private var originalPos : float;
    4. //change this as required
    5. var openPos : float=-90;
    6. private var triggered = false;
    7. private var opening = false;
    8. var openingSpeed: float;
    9.  
    10. function Start(){
    11.  
    12. origionalPos=door.localEulerAngles.y;
    13.  
    14. }
    15.  
    16. function Update () {
    17.  
    18. if(opening)
    19. {
    20. var angle : float = Mathf.LerpAngle(door.localEulerAngles.y, openPos, openingSpeed);
    21. door.localEulerAngles = Vector3(0, angle, 0);
    22. }
    23.  
    24. }
    25.  
    26.  
    27. function OnTriggerEnter (other : Collider) {
    28.  
    29. if(other.CompareTag("Player"))
    30. {
    31. //Cleanup
    32. if(triggered)
    33. Destroy(gameObject);
    34.  
    35. opening=true;
    36. triggered=true;
    37.  
    38. }
    39.  
    40. }
     
    Last edited: Aug 19, 2015