Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Closing and Opening door animation

Discussion in 'Animation' started by Atix07, Jun 10, 2014.

  1. Atix07

    Atix07

    Joined:
    Jul 27, 2013
    Posts:
    182
    Hello! I have 2 animations called Opening door and closing door so when I press the ("E") button ı want to play animation. How I can do that. Im using animator in the door object. Im pretty new in the animations. So please help :)
     
  2. NickP_2

    NickP_2

    Joined:
    Jul 9, 2013
    Posts:
    61
    Are you new to c# and unity? Like do you know how to work with animators in scripting?
     
  3. Atix07

    Atix07

    Joined:
    Jul 27, 2013
    Posts:
    182
    @NickP_2 Im not new to Unity or c# but ım new to animations. Can you help me please. So I made bool parameter in the animator and connected them in animator and I got component of animator in the script but I dont know how to change bool parameter true or not.
     
    Last edited: Jun 11, 2014
  4. NickP_2

    NickP_2

    Joined:
    Jul 9, 2013
    Posts:
    61
    Im on my phone so sorry if i cant put code directly.

    You need to make a public variable of an animator; lets say you name it "a". So it looks like "public Animator a;"

    Now you can change the bool with:

    "A.SetBool("name", "true or false");"

    The name is the name you gave the bool in the animator window, and "true or false" is the bool you give as a parameter, just true, or just false.

    Same for floats (a.SetFloat(name, float)) or int (a.SetInt(name, int))

    Hope it helped
     
  5. NickP_2

    NickP_2

    Joined:
    Jul 9, 2013
    Posts:
    61
    Oh and drag the animator into the public variable in the scripts inspector
     
  6. Atix07

    Atix07

    Joined:
    Jul 27, 2013
    Posts:
    182
    Owwww my goddd! Thank you sooooooo much! Finally I understand! Thank youu so much! U are my hero!
     
  7. Atix07

    Atix07

    Joined:
    Jul 27, 2013
    Posts:
    182
    1 More Question... @NickP_2 When I duplicate the door 2. door is moving to the first doors transform. Because I made the door animation by changing the position and the rotation. Animations position is the first doors position. Do I need to make animation every time? Or there is clever way to do it?
     
  8. NickP_2

    NickP_2

    Joined:
    Jul 9, 2013
    Posts:
    61
    Put the original door is a prefab and duplicate the prefab, that should do it. Dont just copy paste it in the game hierachy :) if that didnt work just tell me
     
  9. Atix07

    Atix07

    Joined:
    Jul 27, 2013
    Posts:
    182
    @NickP_2 Putting in prefab dont work. Still teleporting to the same position of orginal door...
     
  10. NickP_2

    NickP_2

    Joined:
    Jul 9, 2013
    Posts:
    61
    Hmh, If I private you my gmail, could you send me the script? or place it here, ill take a look at it. I'm not 100% up to date about what code you used :)
     
  11. NickP_2

    NickP_2

    Joined:
    Jul 9, 2013
    Posts:
    61
    Oh did you animate its localposition and not worldposition?
     
  12. Atix07

    Atix07

    Joined:
    Jul 27, 2013
    Posts:
    182
    @NickP_2 Hello! Sorry for late respond. Here is my code:
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class KapıAcmakapama : MonoBehaviour
    6. {
    7.     public Transform player;
    8.  
    9.     bool kapıkapalı = true;
    10.     bool yakın = false;
    11.  
    12.     public Animator Anim;
    13.  
    14.     // Update is called once per frame
    15.     void Update ()
    16.     {          
    17.         if (Vector3.Distance(transform.position,player.position) < 3f & kapıkapalı == true)
    18.         {
    19.             yakın = true;
    20.  
    21.             if (Input.GetKeyDown(KeyCode.E))
    22.             {
    23.                 kapıkapalı = false;
    24.                 Anim.SetBool("OpenBool", false);
    25.             }
    26.         }
    27.         else if (Vector3.Distance(transform.position, player.position) < 3f & kapıkapalı == false)
    28.         {
    29.             yakın = true;
    30.        
    31.             if (Input.GetKeyDown(KeyCode.E))
    32.             {
    33.                 kapıkapalı = true;
    34.                 Anim.SetBool("OpenBool", true);
    35.             }
    36.         }
    37.         else
    38.         {
    39.             yakın = false;
    40.         }
    41.     }
    42.     void OnGUI ()
    43.     {
    44.         if (yakın == true && kapıkapalı == true)
    45.         {
    46.             GUI.Label(new Rect(600, 100, 100, 20), "Press E to Open");
    47.         }
    48.         else if (yakın == true && kapıkapalı == false)
    49.         {
    50.             GUI.Label(new Rect(600, 100, 100, 20), "Press E to Close");
    51.         }
    52.     }
    53. }
    54.  
    Well door is working but with only 1 door. I mean ı can duplicate or copy it. I think I made animations by world position and I should change it to local position.

    Edit: Let me translate the words which is not english in the scirpt.
    Yakın = Near
    KapıKapalı = Door is Closed
     
    Last edited: Jun 19, 2014
  13. NickP_2

    NickP_2

    Joined:
    Jul 9, 2013
    Posts:
    61
    No problem :)

    The code seems fine on the first look. But Did you animate the door in 3rd pary software or with the animation build in thing in Unity?
     
  14. Atix07

    Atix07

    Joined:
    Jul 27, 2013
    Posts:
    182
    Okay I found it finally! I made that animations in the Unity. The problem is about door transfrom. Every time animation played its teleporting to the same position as where animation is created. So I placed a empty game object and I make door its child. I animated all animations again andd tada! Thanks for everythink @NickP_2. I understand animations by you. Thanks alot!
     
    NickP_2 likes this.
  15. programer717

    programer717

    Joined:
    Oct 9, 2017
    Posts:
    12
    Hia! This is kind of random but I tried doing something similar to this, I made a jail cell with bars that magickly move when you press the magic button. The problem is even after making the cell as prefabs, when I duplicate the jail cell and move it to the left in the editor and click play. I run over to the button click it (I'm using raycasts) it works but when I run to the other cell and click the button the 1st door (the duplicated one) closes, instead of the 2nd door (the original door) opening. I am confused on why this is happening.