# How to make a door stay open if the player is inside the trigger zone?

I've been struggling with this for all day long, I want to make a door to stay open when the player is inside the trigger zone, but I cannot make it happen...

Here is what I did:

• A basic animations and animator (Open, Close, Idle/Default)
• Default transitions between states (with and without "Exit Time")
• A basic script to check if the player is inside the trigger:

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

public class DoorScript : MonoBehaviour
{
Animator _animator;
bool doorOpen;

// Use this for initialization
void Start ()
{
doorOpen = false;
_animator = GetComponent<Animator>();
}

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

} */

void OnTriggerEnter(Collider other)
{
if (other.tag == "Player")
{
doorOpen = true;
DoorController("isOpen");
}
}

void OnTriggerExit(Collider other)
{
if (other.tag == "Player")
{
doorOpen = false;
DoorController("isClosed");
}
}

void DoorController(string trigger)
{
_animator.SetTrigger(trigger);
}
}


And my result is the same, the animation of the door repeats itself even when the player is in the trigger zone, I tumbled upon some other resources (here... here... here... here I tried to copy what this person did, but is the same... here... and here) about doors and triggers, and I still don't know how I am wrong or what am I doing wrong, I tried everything in those links

The door goes through all animations regardless the script, and the parameters, when I move my player inside the trigger zone loops between open and close animations no matter what

Could some one help me with this?

EDIT:

Here are some screenshots of my animator and updated code, for this update I followed this tutorial, just a little difference, I'm using a FPS Controller from the Unity standard assets

Updated code:

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

public class DoorScript : MonoBehaviour
{
Animator _animator;
bool doorOpen;

// Use this for initialization
void Start ()
{
doorOpen = false;
_animator = GetComponent<Animator>();
}

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

} */

void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Player")
{
doorOpen = true;
DoorController("isOpen");
}
}

void OnTriggerExit(Collider other)
{
//These lines were modified
if (doorOpen)
{
doorOpen = false;
DoorController("isClosed");
}
}

void DoorController(string trigger)
{
_animator.SetTrigger(trigger);
}
}

• The door should only close when you leave ( exit ) the trigger. So I'm not understanding why it closes on its own? Unless the animation switches the state automatically? You should probably add a condition between the open and close transition. something like a canClose bool and set that to true when you leave the trigger. As long there is no exit on your collision canClose should always be false once you enter the trigger. – Sidar Jan 3 '18 at 6:02
• Yes, I want to make the door close automatically after leaving the trigger zone, and yes again, the animation states switch automatically – NAYIR55 Jan 3 '18 at 8:00
• Well have you tried making a simple condition between open and closed state and setting that condition when you exit the trigger? – Sidar Jan 3 '18 at 10:03
• Please post a screenshot of your animation state machine. I think the issues is with the transitions. If you don't tie the transitional arrows to a variable, they will traverse freely. you only want to go from your closed to open state when open ==true, and from open to closed state when open==false. – Stephan Jan 3 '18 at 14:18
• Please check the EDIT info – NAYIR55 Jan 3 '18 at 18:24

Seems that other objects can trigger the door closing if they exit the trigger zone while the player is in it. Try using if (other.gameObject.tag == "Player") with the onTriggerExit(), too.

• You should explain your reasoning and how the solution fits into the code. As it is, this is not a useful answer in my opinion. – John Hamilton Jan 4 '18 at 12:35
• @JohnHamilton Edited. Is it clearer now? – Crestwave Jan 4 '18 at 23:50
• Much better, I'm glad I chose "Looks OK" instead of "Recommend Deletion" – John Hamilton Jan 5 '18 at 5:44

you have to make the trigger to fill the who room. onTriggerEnter() you have to play door-open animation and onTriggerExit() you have to play door-close animation. there are a lot of approaches to do this. I just told one of them.

sorry for careless answer. I edit. repeating animation is because loop time of animation is checked and you have to uncheck it. other reason can be that your trigger is colliding with other objects. you can simple ignore them by making different layers for them.

• The use of trigger enter and exit is already being used. I think OP is having trouble with state transitions. – Sidar Jan 3 '18 at 6:37
• Yes, I'm having trouble with state transitions, because the animations play automatically when my player is inside the trigger zone for the door, it should play only the corresponding animation – NAYIR55 Jan 3 '18 at 8:01

Solved! I'm so stupid! In the end, the trigger zone was very small and my player collided everytime and that's why the door opened and closed like crazy... I'm really thankful for all your answers and comments