Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Please help me create a sliding door animation in Unity.I'm a beginner and don't understand well.

Discussion in 'Animation' started by Seeanax, Oct 6, 2023.

  1. Seeanax

    Seeanax

    Joined:
    Nov 6, 2022
    Posts:
    8
    Please help me create a sliding door animation in Unity. I'm a beginner in this field and don't have a good understanding of how to do it. Please.

    Hello, can you help me create an animation for a door? I want to make it so that the door can be opened by pressing the "E" key on the keyboard when you are inside the button colliders and looking at them. In other words, the player should only be able to press "E" if they are inside the button colliders and looking at them at that moment. After pressing, the "DoorController" script checks the state of the door (initially, they are closed, but for some reason, the animator plays the opening animations, as shown in the video). If the door is open, it plays the closing animation (I have two of them, "DoorClose1" and "DoorClose2," and two opening animations, "DoorOpen1" and "DoorOpen2"), and it plays the sound that I will specify later in the inspector. The animation should play only once, meaning that when the door transitions from the open state to the closed state through the animation, the animation is played once and remains in the position indicated in the last frame of the animation. Also, how can I add a delay between button presses? Let's say, introduce a 2-3 second delay before allowing another button press.

    Video:


    Code:

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class DoorController : MonoBehaviour
    4. {
    5.     public GameObject player; // Объект игрока
    6.     public Animator doorAnimator1; // Аниматор первой двери
    7.     public Animator doorAnimator2; // Аниматор второй двери
    8.     public KeyCode interactKey = KeyCode.E; // Клавиша для взаимодействия с дверью
    9.  
    10.     private bool isOpen = false; // Состояние дверей
    11.     private bool isPlayerNearButton = false; // Находится ли игрок около кнопки
    12.  
    13.     private void Update()
    14.     {
    15.         if (isPlayerNearButton && Input.GetKeyDown(interactKey))
    16.         {
    17.             if (isOpen)
    18.             {
    19.                 CloseDoors();
    20.             }
    21.             else
    22.             {
    23.                 OpenDoors();
    24.             }
    25.         }
    26.     }
    27.  
    28.     private void OpenDoors()
    29.     {
    30.         doorAnimator1.SetBool("isOpen", true);
    31.         doorAnimator2.SetBool("isOpen", true);
    32.         isOpen = true;
    33.     }
    34.  
    35.     private void CloseDoors()
    36.     {
    37.         doorAnimator1.SetBool("isOpen", false);
    38.         doorAnimator2.SetBool("isOpen", false);
    39.         isOpen = false;
    40.     }
    41.  
    42.     private void OnTriggerEnter(Collider other)
    43.     {
    44.         if (other.gameObject == player)
    45.         {
    46.             isPlayerNearButton = true;
    47.         }
    48.     }
    49.  
    50.     private void OnTriggerExit(Collider other)
    51.     {
    52.         if (other.gameObject == player)
    53.         {
    54.             isPlayerNearButton = false;
    55.         }
    56.     }
    57. }
    58.  
     
  2. unity_420E2F030C71A834B25B

    unity_420E2F030C71A834B25B

    Unity Technologies

    Joined:
    Aug 20, 2021
    Posts:
    7
    Hello, your script appears to work, but for the OnTriggerEnter() and OnTriggerExit() events to fire, either your "Player" object or the object with the Collider needs to have a RigidBody component. More info is here. If those events don't fire, your isPlayerNearButton variable never gets set to true.

    For the delay between button clicks, you could do something like this:

    Code (CSharp):
    1.  
    2.     private float DelayTime = 2f; // Time in seconds to wait between presses
    3.     private float LastInteractTime = 0f; // Stores the time of last press
    4.  
    5.     private void Update()
    6.     {
    7.         bool canClick = (Time.time - LastInteractTime) > DelayTime; // Check the time since last pressed
    8.         if (canClick && isPlayerNearButton && Input.GetKeyDown(interactKey))
    9.         {
    10.             LastInteractTime = Time.time; // Set the LastInteractTime
    11.             if (isOpen)
    12.             {
    13.                 CloseDoors();
    14.             }
    15.             else
    16.             {
    17.                 OpenDoors();
    18.             }
    19.         }
    20.     }
    21.  
    While testing/debugging, it is helpful to add Debug.Log("My custom message"); statements in your code to ensure that various functions are firing as expected (for performance reasons, you should remove these logging statements when you verify it is functioning as intended).
     
    Last edited: Oct 12, 2023
    Seeanax likes this.
  3. Seeanax

    Seeanax

    Joined:
    Nov 6, 2022
    Posts:
    8


    Thanks a lot! Could you please help me on how to properly create animations for shooting/reloading/or simply when the player does nothing with the weapon. It’s just that I made a weapon and it shoots and can reload (if necessary, I’ll post a video there and show more information about the weapon), I was able to make a reloading animation for it and everything is ok, but with shooting I just can’t do everything correctly, then the animation plays every other time, that is, I I make a shot and it may or may not lose, then 2 and also I’m completely confused and don’t understand how to fix this, please help, I really need it.