Search Unity

Question Have a bit of a problem with door animations

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

  1. Kekeshi

    Kekeshi

    Joined:
    Oct 19, 2022
    Posts:
    5
    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

    Unrighteouss

    Joined:
    Apr 24, 2018
    Posts:
    599
    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: https://drive.google.com/file/d/1CbXwVRcrwXhxRBl7DFJlO9Ve7LItGbhL/view?usp=share_link

    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;
    2.  
    3. public class DoorOpenClose : MonoBehaviour
    4. {
    5.     public bool doorOpen = false;
    6.  
    7.     Animator anim;
    8.  
    9.     void Start()
    10.     {
    11.         anim = GetComponent<Animator>();
    12.     }
    13.  
    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

    Kekeshi

    Joined:
    Oct 19, 2022
    Posts:
    5
    Thanks for the reply! I will try your solution.