Search Unity

Question How can i make my character go back to idle after opening a door?

Discussion in 'Scripting' started by PRVSNL, Mar 29, 2023.

  1. PRVSNL

    PRVSNL

    Joined:
    Mar 31, 2022
    Posts:
    3
    I have a 3rd person game and i made some animations to open a door and in my door code it gets the animator of both the door and player to play 2 animations: 1 the door opening and 2 the player opening the door.
    But after both are done the players animation doesn't return to idle.
    it goes back to the first frame of the door open animation of the player.

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class DoorController : MonoBehaviour
    4. {
    5.     public Animator animator;
    6.     public Animator playerAnimator;
    7.  
    8.     private void OnTriggerEnter(Collider other)
    9.     {
    10.         if (other.CompareTag("Player"))
    11.         {
    12.             Debug.Log("Player entered trigger");
    13.         }
    14.     }
    15.  
    16.     private void OnTriggerExit(Collider other)
    17.     {
    18.         if (other.CompareTag("Player"))
    19.         {
    20.             Debug.Log("Player left trigger");
    21.         }
    22.     }
    23.  
    24.     private void Update()
    25.     {
    26.         if (Input.GetKeyDown(KeyCode.E) && animator != null && animator.isActiveAndEnabled)
    27.         {
    28.             animator.SetTrigger("OpenDoor");
    29.  
    30.             if (playerAnimator != null && playerAnimator.isActiveAndEnabled)
    31.             {
    32.                 playerAnimator.SetTrigger("OpenDoor");
    33.             }
    34.         }
    35.     }
    36.  
    37.    
    38. }
    39.  
     
  2. PRVSNL

    PRVSNL

    Joined:
    Mar 31, 2022
    Posts:
    3
    i tried a lot of ways to set a new trigger to when the door was opened but nothing worked, it either just looped all my animations or did nothing so here's my starting code