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. Dismiss Notice

Question Lift doors animation problem

Discussion in 'Animation' started by Argonxx, Aug 5, 2023.

  1. Argonxx

    Argonxx

    Joined:
    Apr 16, 2017
    Posts:
    37
    Hello,
    I am trying to make animation for two lift doors, but it works only in half.
    I have two animations for opening and closing doors.

    Doors are a child of a trigger. Trigger should work when I will press E button. And it works but only for left doors. Please look at the code and screen.

    Code (CSharp):
    1. public class LiftDoorsController : MonoBehaviour
    2. {
    3.     public GameObject ThisTrigger;
    4.    
    5.     [Header("Audio")]
    6.     [SerializeField] private AudioSource doorOpenAudioSource = null;
    7.     [SerializeField] private float openDelay = 0;
    8.     [Space(10)]
    9.     [SerializeField] private AudioSource doorCloseAudioSource = null;
    10.     [SerializeField] private float closeDelay = 0;
    11.    
    12.    
    13.     private void OnTriggerStay(Collider other)
    14.     {
    15.         if(other.tag == "LiftDoor")
    16.         {
    17.             Animator anim = other.GetComponentInChildren<Animator>();
    18.             if(Input.GetKeyDown(KeyCode.E))
    19.             anim.SetTrigger("LiftOpenClose");
    20.             doorOpenAudioSource.PlayDelayed(openDelay);
    21.             print("DoorLiftTrigger");
    22.         }
    23.     }
    24. }
    screen-lift1.jpg
     
  2. tsukimi

    tsukimi

    Joined:
    Dec 10, 2014
    Posts:
    50
    Animator anim = other.GetComponentInChildren<Animator>();
    This only get one animator (=> LiftDoors.L). you could use
    Animator[] anims = other.GetComponentsInChildren<Animator>();
    to get all animators.
     
  3. Argonxx

    Argonxx

    Joined:
    Apr 16, 2017
    Posts:
    37
    Ok, but it says "(23,13): error CS0103: The name 'anim' does not exist in the current context"

    23. anim.SetTrigger("LiftOpenClose");

    I added
    private Animator anim;
    and now there is no error but it still doesn't work.
     
  4. tsukimi

    tsukimi

    Joined:
    Dec 10, 2014
    Posts:
    50
    I mean this:
    Code (CSharp):
    1.     private void OnTriggerStay(Collider other)
    2.     {
    3.         if(other.tag == "LiftDoor")
    4.         {
    5.             if (Input.GetKeyDown(KeyCode.E)) {
    6.                 // get all animators under door..., but why is the original script getting it from "other"?
    7.                 Animator[] anims = other.GetComponentsInChildren<Animator>();
    8.                 foreach (Animator anim in anims) {
    9.                     anim.SetTrigger("LiftOpenClose");
    10.                 }
    11.                 doorOpenAudioSource.PlayDelayed(openDelay);
    12.                 print("DoorLiftTrigger");
    13.             }
    14.         }
    15.     }
    also there are other details you didn't show, like "What is the purpose of LiftDoorsController? Are you attaching it on your player, or on the door?", and "Why is there another script of
    LiftDoorsControllerR
    , is it doing something?".
     
  5. Argonxx

    Argonxx

    Joined:
    Apr 16, 2017
    Posts:
    37
    Yes, the player has also DoorController script.
    LiftDoorsControllerR is not in use. I have just created it for a second door, thinking that maybe it will work. But it makes nothing.
    LiftDoorsController is only one in use.
     
  6. Argonxx

    Argonxx

    Joined:
    Apr 16, 2017
    Posts:
    37
    After using your suggestion there is no print("DoorLiftTrigger"); Nothing happens.
     
  7. Argonxx

    Argonxx

    Joined:
    Apr 16, 2017
    Posts:
    37
    Ok, I have it! But thanks to the ChatGPT. After understanding the problem he wrote a different code, which corresponds to both animators.