0
\$\begingroup\$

so i want to open a door with a keypress but it's not changing the 2 bool's state any help?

public Animator _animator;

void Start ()
{
    _animator = GetComponent<Animator>();
    _animator.SetBool("close", true);
    _animator.SetBool("Open", false);
}


void OnTriggerStay(Collider other)
{
    if (other.tag == "player")
    {
        if (Input.GetKeyDown(KeyCode.E))
        {
            _animator.SetBool("close", false);
            _animator.SetBool("Open", true);
        }
    }
}

enter image description here

\$\endgroup\$
  • 2
    \$\begingroup\$ A few suggestions, it would be better if you edited the title of your question to be an actual question of your problem, then we would need some more info to help you. When you say "it doesn't work" does that mean that the door is not moving at all? Are you certain that the animation on the door is properly opening/closing the door but it's just not firing from the code? A screenshot of the Animator and how its configured would really help \$\endgroup\$ – TomTsagk Aug 23 '18 at 10:01
  • \$\begingroup\$ yes it dosen't change the animation state "close to false and open to true" \$\endgroup\$ – MonsterLHGoO Aug 23 '18 at 10:15
  • \$\begingroup\$ but it's working if i use with OnTriggerEnter and no key used but i want this to work when i press the E key \$\endgroup\$ – MonsterLHGoO Aug 23 '18 at 10:16
  • \$\begingroup\$ The first thing I would do to troubleshoot this problem is to find out if the OnTriggerStay function actually gets called. Did you verify this? Also, keep in mind that tags are case-sensitive. "player" is not the same tag as "Player". \$\endgroup\$ – Philipp Aug 23 '18 at 10:47
  • \$\begingroup\$ omg i'm an idiot the problem was the "player"tag :D i didn't noticed that thx so much \$\endgroup\$ – MonsterLHGoO Aug 23 '18 at 10:53
0
\$\begingroup\$

I think the problem is being caused by OnTriggerStay() with the GetKeyDown().

Try this:

  • Create a new variable

    bool isPlayerHere = false;

  • Use OnTriggerEnter(), if the player caused it to fire (with the other.tag like you are already doing), set isPlayerHere to true

  • Then OnTriggerExit() will set the isPlayerHere to false.
  • Now on your Update(), check if isPlayerHere is true, then check for input.

Example code:

// Player just entered the collider, so they are "near" the door
void OnTriggerEnter(Collider other)
{
    if (other.tag == "player")
    {
        isPlayerHere = true;
    }
}

// Player just exited the collider
void OnTriggerExit(Collider other)
{
    if (other.tag == "player")
    {
        isPlayerHere = false;
    }
}

void Update() {

    // Player is inside the collider that can activate the door,
    // and also pressed the right key - activate the door
    if (isPlayerHere && Input.GetKeyDown(KeyCode.E))
    {
        _animator.SetBool("close", false);
        _animator.SetBool("Open", true);
    }
}

As a side note, I could also suggest, instead of having 2 variables "close" and "Open", you could just only one named open, if its value is false then the door is closed, if its true then the door is open. (this also simplifies the Animator implementation)

\$\endgroup\$
  • \$\begingroup\$ thx that really helps me out but the problem was i used the player tag not the Player :D \$\endgroup\$ – MonsterLHGoO Aug 23 '18 at 10:55
  • \$\begingroup\$ @MonsterLHGoO Ouch! haha, well I'm glad the problem is solved :) \$\endgroup\$ – TomTsagk Aug 23 '18 at 11:02

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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