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

Request: C# Script for Opening Doors

Discussion in 'Scripting' started by Rockinator, Jul 25, 2014.

  1. Rockinator

    Rockinator

    Joined:
    Jul 25, 2014
    Posts:
    22
    I'm a complete novice with scripting and their is much for me to learn; for right now I wanted to start with something simple like opening and closing doors. What I am requesting is a script that will open a door with a few conditions that are adjustable within the editor.

    when a key is pressed it will open the door, if the door is already opened the same key will close the door. I would also like for the script to allow me the flexibility to allow me to change what animations I would like to use by dragging said animations into the editor. One last thing, I would like for the script to allow me the use of keys (like an iron key, in game item) if possible, I would just have difficulty trying to understand how to get the game to recall if said item was collected by the player. Like I said, I am a complete novice!

    Thanks in advance for the assistance! Oh, I would like to reference your script if you so wish it! Thanks!
     
  2. SubZeroGaming

    SubZeroGaming

    Joined:
    Mar 4, 2013
    Posts:
    1,008
    Here's the pseudo code:

    //bool for door open
    //int for key


    //if key is pressed and key >= 1
    //OpenDoor() //function to open/close door

    void OpenDoor()
    {
    //if doorOpen = true
    //animation to close door (animation.Play)
    //doorOpen = false;
    else
    //animation to open door
    //doorOpen = true;
    }

    I hope nobody just spoon feeds you. This is a very simple logic task to complete.

    Best of luck,

    Jon
     
    raycosantana and landon912 like this.
  3. Rockinator

    Rockinator

    Joined:
    Jul 25, 2014
    Posts:
    22
    it is simple logic for one who understands how to formulate code though I can see this logic in what you wrote but practical application is a different story. I appreciate the assist.
     
    SubZeroGaming likes this.
  4. SubZeroGaming

    SubZeroGaming

    Joined:
    Mar 4, 2013
    Posts:
    1,008
    Assuming you have legacy animations for opening and closing the door:

    You need to create a variable that checks if the door is open. That's where the bool comes in. It returns true or false.

    public bool doorOpen;

    You mentioned you wanted a key feature? You can do that based on x amount of keys or 1 key. When the user gets the key, increment the value or turn a bool true.

    public int key;

    Now that you have your variables, you need to access the animation component that has the animation. Create a private handle:

    private Animation _anim;

    Now in void Start() assign that handle to the Component.

    void Start()
    {
    _anim = GetComponent<Animaton>();
    }

    Then, you want to make functionality to press a key and check to see if they have the key:

    if (Input.GetKeyDown(KeyCode.Q) && key > 0) // key greater than 0 means they have at least 1 key
    {
    OpenDoor();
    }

    void OpenDoor()
    {
    //create logic for if it's already open. If it is, close it.
    if (doorOpen == true)
    {
    _anim.Play("ClosingAnimation");
    doorOpen = false;
    else
    {
    _anim.Play("OpenAnimation");
    doorOpen = true;
    }
    }
    }
     
  5. Rockinator

    Rockinator

    Joined:
    Jul 25, 2014
    Posts:
    22
    wow, I actually understood all of that! I have but one remaining issue now, you mentioned legacy animation, I know of the method of animation I used to make the door pivot to simulate the opening and closing animations. When I added animation curves it was through the empty game object I made that was parenting the door cube. Am I still to use the collider I made to be the triggering point for the animation or I only must click on the door? by the by, your help has been the best I've experienced since starting this project.
     
  6. SubZeroGaming

    SubZeroGaming

    Joined:
    Mar 4, 2013
    Posts:
    1,008
    Legacy animation is the type.
    On the gameObject that is acting as your door, what component is attached?
    Animation or Animator?

    If it's Animator, I recommend changing it to Legacy as it may be more simplistic for you.

    To change it from Animator to Animation, remove the Animator component,

    and add an Animation Component
    Now you need to click on your animation that you created which should be in the project view, select it, and at the top, there is a drop down. You need to go into debug mode. (research how to go in debug mode or research conversion from mechanim to legacy) Once in debug mode, you'll see Animation type says : 2. Change it to 1. That will allow you to use Legacy animation giving you the ability to type _anim.Play.

    Best of luck,

    Jon
     
  7. SubZeroGaming

    SubZeroGaming

    Joined:
    Mar 4, 2013
    Posts:
    1,008
    Also, yes. Keep your colliders, if you're checking for a collider, just continue doing OnTriggerEnter or whatever you're using and call the OpenDoor function at that time.
     
  8. ama7520

    ama7520

    Joined:
    Jun 13, 2015
    Posts:
    2
    where i have to put this script?
    on my character or the door? please i still confused

    i still newbie
     
  9. Korno

    Korno

    Joined:
    Oct 26, 2014
    Posts:
    518
    What code? There is only psuedo code here.
     
  10. SubZeroGaming

    SubZeroGaming

    Joined:
    Mar 4, 2013
    Posts:
    1,008
    lol....this is from 11 months ago this thread, and as Korno pointed out...it's pseudo code...Please check my signature for some interactive tutorials. You need them.