Search Unity

Script for open and close door animation with the same clic?

Discussion in 'Scripting' started by Sim_Mad, Oct 8, 2018.

  1. Sim_Mad

    Sim_Mad

    Joined:
    Mar 26, 2018
    Posts:
    4
    (watch video)

    By clicking on the active door the door opening animation. I would now like it to close with the same click.
    How can I do that? Is there a script that activates one animation and then starts another if it has already been activated?
    Or do you have other solutions?

    this is code I use:

    Code (CSharp):
    1. using System.Collections;
    2.  
    3. using System.Collections.Generic;
    4.  
    5. using UnityEngine;
    6.  
    7.  
    8.  
    9. public class OpenCloseDoor : MonoBehaviour {
    10.  
    11.  
    12.  
    13. Animator anim;
    14.  
    15.  
    16.  
    17. void Start()
    18.  
    19. {
    20.  
    21. anim = gameObject.GetComponent<Animator>();
    22.  
    23. }
    24.  
    25. void Update()
    26.  
    27. {
    28.  
    29. if (Input.GetMouseButtonDown(0))
    30.  
    31. {
    32.  
    33. anim.SetTrigger("open");
    34.  
    35. }
    36.  
    37.  
    38.  
    39. }
    40.  
    41.  
    42.  
    43. }
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Just set a bool that says if the door is opened or closed. If it is closed, when you click you do the open animation, if the bool says it is open you run the closed animation.

    Code (csharp):
    1. private bool doorIsClosed = true;
    2.  
    3. void Update ()
    4. {
    5.     if (Input.GetMouseButtonDown(0))
    6.     {
    7.         if (doorIsClosed)
    8.         {
    9.             //run the door open animation here
    10.  
    11.             doorIsClosed = false;
    12.         }
    13.         else
    14.         {
    15.             //run the door closing animation here
    16.  
    17.             doorIsClosed = true;
    18.         }
    19.     }
    20.  
    21. }
     
    Franciscotx56 likes this.
  3. Cyber-Dog

    Cyber-Dog

    Joined:
    Sep 12, 2018
    Posts:
    352
    The above can be shortened by writing
    doorIsClosed = !doorIsClosed;
    But I reckon you can do this a better way.

    What I recommend is having two animations in your animator, Opened and Closed. Set the default to closed. Then put a transition back and forth between Opened and Closed.

    On you mouse click, call anim.SetTrigger("Interact");

    So if the door is open and you trigger interact it will start the close, of its closed it will start the open.

    Remembering the default is door closed, if you want the game to start with the dore open then just call anim.SetTrigger("Interact"); in your Start() function.
     
    Joe-Censored likes this.
  4. Cyber-Dog

    Cyber-Dog

    Joined:
    Sep 12, 2018
    Posts:
    352
    Here is the Animator example
     

    Attached Files:

  5. Sim_Mad

    Sim_Mad

    Joined:
    Mar 26, 2018
    Posts:
    4
    Thankyou everyone! Very useful! My main problem is that, by modeling, I can make something graphically, but I have many shortcomings in programming c#. I'm trying to learn from the beginning.
     
    Cyber-Dog likes this.
  6. Cyber-Dog

    Cyber-Dog

    Joined:
    Sep 12, 2018
    Posts:
    352
    No worries man, I'm happy to help :)
     
  7. Sim_Mad

    Sim_Mad

    Joined:
    Mar 26, 2018
    Posts:
    4


    I'm happy :)
     
  8. Cyber-Dog

    Cyber-Dog

    Joined:
    Sep 12, 2018
    Posts:
    352
    Hey man,

    I just realized with your first script posted. There is no distance check to ensure the player cant open the dorr from to far away. If you looking to achieve that here is some sudo that you can reference to get it working.

    Code (CSharp):
    1. private into minDistance = 10;
    2. private Transform playerTrans;
    3.  
    4. start()
    5. {
    6.     playerTrans = GameObject.FindByTag("Player").tranform;
    7. }
    8.  
    9.  
    10. Update()
    11. {
    12.     if (Input.GetMouseButtonDown(0))
    13.     {
    14.         if (Vector3.Distance(this.gameObject.transform.position, playerTrans.position) <= minDistance)
    15.         {
    16.             //Do stuff Here
    17.         }
    18.     }
    19. }
     
  9. You're opening the door on the wrong side... :grinch: (sorry ;) )
     
  10. Sim_Mad

    Sim_Mad

    Joined:
    Mar 26, 2018
    Posts:
    4
    Absolutely! I even better arrange the dimensions of the tower and door! :)



    REALLY REALLY THANK YOU MAN!!!
     
    Cyber-Dog likes this.