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. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

Open Door Issue. Close animation plays right after open animation sometimes

Discussion in 'Animation' started by dom_cobb_22, Sep 1, 2018.

  1. dom_cobb_22

    dom_cobb_22

    Joined:
    Feb 6, 2014
    Posts:
    24
    Hello everyone.

    I am just crying right now. I have tried to solve this many times. But nothing that I can.

    Now I going to descript all of this process...

    What I need: just door to open and close.

    How do I approximate this:

    I have a model, have a DoorController on it. It has a tag to choose from three couple different animations depends on a number of degrees to rotate (Door95, Door105, Door120).
    Next, I have several animation clips for different angles (Door Open 95, Door Close 95, Door Open 105, Door Close 105, Door Open 120, Door Close 120).
    Also, I have Animator Controller. It has triggers: OpenDoor95, CloseDoor95, OpenDoor105, CloseDoor105, OpenDoor120, CloseDoor120.
    Each animation clips starts with this triggers. Below, you can see images of how this oriented. Door Close animations have exit time.

    Next, I have Player with Box Collider -> Is Trigger checked and it has a tag -> Player.

    Doors have a DoorController script and that is what it contains:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class DoorController : MonoBehaviour {

    private Animator m_animator;
    private bool open;

    // Use this for initialization
    void Start () {
    open = false;
    this.m_animator = GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update () {

    }

    public void OnTriggerStay(Collider other)
    {
    if (Input.GetButtonDown("Use") && other.tag == "Player")
    {
    if (!open)
    {
    switch (gameObject.tag)
    {
    case "Door95":
    m_animator.SetTrigger("OpenDoor95");
    break;
    case "Door105":
    m_animator.SetTrigger("OpenDoor105");
    break;
    case "Door120":
    m_animator.SetTrigger("OpenDoor120");
    break;
    default:
    break;
    }

    Debug.Log("OPEN DOOR");
    }
    else
    {
    switch (gameObject.tag)
    {
    case "Door95":
    m_animator.SetTrigger("CloseDoor95");
    break;
    case "Door105":
    m_animator.SetTrigger("CloseDoor105");
    break;
    case "Door120":
    m_animator.SetTrigger("CloseDoor120");
    break;
    default:
    break;
    }

    Debug.Log("CLOSE DOOR");
    }

    open = !open;
    Debug.Log("CHANGE OPEN");
    }
    }

    }


    We have a bool value, and if the door is not opened we open it, and if it's opened we close. Doing it if the player is near to this door and if the key button is down.
    Everything is good. But no.

    The problem is door animation playing twice sometimes. Not one animation, but the animation that next to another. I mean - Door Opens and immediately Close after it.
    I mean - I press the Use button and see how the door opens and right after this its close. Or it closes and opens right after closing.
    I have felt as my "else block" of my code run just after "if block". Or something like this.

    But another time its work fine.

    See the images and video, please.

    Animator.PNG AnimatorItems.png SceneView.png PlayerAndDoor.png OpenDoorConsole.PNG
     
    Last edited: Sep 13, 2018
  2. zeropointblack

    zeropointblack

    Joined:
    Jun 8, 2020
    Posts:
    173
unityunity