0
\$\begingroup\$

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?

Thanks in advance

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

enter image description here

enter image description here

enter image description here

enter image description here

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);
    }
}
\$\endgroup\$
  • \$\begingroup\$ 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. \$\endgroup\$ – Sidar Jan 3 '18 at 6:02
  • \$\begingroup\$ Yes, I want to make the door close automatically after leaving the trigger zone, and yes again, the animation states switch automatically \$\endgroup\$ – NAYIR55 Jan 3 '18 at 8:00
  • \$\begingroup\$ Well have you tried making a simple condition between open and closed state and setting that condition when you exit the trigger? \$\endgroup\$ – Sidar Jan 3 '18 at 10:03
  • \$\begingroup\$ 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. \$\endgroup\$ – Stephan Jan 3 '18 at 14:18
  • \$\begingroup\$ Please check the EDIT info \$\endgroup\$ – NAYIR55 Jan 3 '18 at 18:24
1
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ 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. \$\endgroup\$ – John Hamilton Jan 4 '18 at 12:35
  • \$\begingroup\$ @JohnHamilton Edited. Is it clearer now? \$\endgroup\$ – Crestwave Jan 4 '18 at 23:50
  • \$\begingroup\$ Much better, I'm glad I chose "Looks OK" instead of "Recommend Deletion" \$\endgroup\$ – John Hamilton Jan 5 '18 at 5:44
0
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ The use of trigger enter and exit is already being used. I think OP is having trouble with state transitions. \$\endgroup\$ – Sidar Jan 3 '18 at 6:37
  • \$\begingroup\$ 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 \$\endgroup\$ – NAYIR55 Jan 3 '18 at 8:01
0
\$\begingroup\$

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

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.