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. Join us on Thursday, June 8, for a Q&A with Unity's Content Pipeline group here on the forum, and on the Unity Discord, and discuss topics around Content Build, Import Workflows, Asset Database, and Addressables!
    Dismiss Notice

Question Have a bit of a problem with door animations

Discussion in 'Animation' started by Kekeshi, Feb 27, 2023.

  1. Kekeshi


    Oct 19, 2022
    I have a door with closing and opening animations embedded into an animator and the opening animation is triggered by an other object's trigger event. After 3 seconds, the script changes the integer in the door's animator that closes the door. It's all good, until an AI is triggering the opening animation while it is midway in the closing animation. Then the door closes really fast, so it can start the opening animation from the default state of it's rotation. How can I make it start the opening animation from where the closing animation cut off?
  2. Unrighteouss


    Apr 24, 2018
    I find that with transitions, it's really difficult to get consistent good looking behavior for mechanical stuff like doors. My favorite way of accomplishing this is through a bit of code. I only create an opening animation, and then reverse that animation whenever I need it to close.

    Example video:

    If this looks interesting to you, I'll create a quick guide on how to set this up. I don't know how familiar you are with coding, but you won't have to do much, if any.

    First of all, here's the script:
    Code (CSharp):
    1. using UnityEngine;
    3. public class DoorOpenClose : MonoBehaviour
    4. {
    5.     public bool doorOpen = false;
    7.     Animator anim;
    9.     void Start()
    10.     {
    11.         anim = GetComponent<Animator>();
    12.     }
    14.     void Update()
    15.     {
    16.         if (doorOpen == true)
    17.         {
    18.             if (anim.GetCurrentAnimatorStateInfo(0).normalizedTime < 0f)
    19.             {
    20.                 anim.Play("Door", 0, 0f); // Change "Door" to your animator state name.
    21.             }
    22.             anim.SetFloat("Speed", 1f); // Change "Speed" to the name of your float parameter.
    23.         }
    24.         else
    25.         {
    26.             if (anim.GetCurrentAnimatorStateInfo(0).normalizedTime > 1f)
    27.             {
    28.                 anim.Play("Door", 0, 1f); // Change "Door" to your animator state name.
    29.             }
    30.             anim.SetFloat("Speed", -1f); // Change "Speed" to the name of your float parameter.
    31.         }
    32.     }
    33. }
    This goes on the door with the animator (I included it as an attachment if you don't want to create it yourself).

    To get it to work, you need to create a float parameter in your animator and set it as the multiplier for your animation state.

    1. Create float parameter (mine is named Speed).
    2. Click on the animation state -> in the inspector tick the Parameter box for Multiplier -> Select Speed.

    It's important that the names of your animation state and parameter match those in the script. Either copy my names, or change them to match yours in the script (my state is called "Door").

    If you decide not to use this, don't worry about it, but if you need help with anything, please let me know.

    Attached Files:

    renem and Kekeshi like this.
  3. Kekeshi


    Oct 19, 2022
    Thanks for the reply! I will try your solution.