,Key immediately registered

Hi! I am trying to create a code, so the player can take out the map, look at it and after that put it away. I managed to create this:

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

public class mapcontroll : MonoBehaviour

{
    [SerializeField] private bool triggerActive = false;
    [SerializeField] private Animator myanimatorcontroller;

    private void FixedUpdate()
    {
        if (!triggerActive && Input.GetKeyDown("m"))
        {
            myanimatorcontroller.SetBool("mapin", true);
            myanimatorcontroller.SetBool("mapout", false);
            triggerActive = true;
            Debug.Log("trigger active");
        }
    }
    private void Update()
    {
        if (triggerActive && Input.GetKeyDown("m"))
        {
                myanimatorcontroller.SetBool("mapout", true);
                myanimatorcontroller.SetBool("mapin", false);
                triggerActive = false;
                Debug.Log("trigger not active");
        }       
    }
}

When I pressed the key nothing happened. So I added the Debug.Log part, so I can figure out where the problem is.

I found out this way, that when I press the “m” key, both part of the script activate at the same time. So the map starts appearing but also goes back immeadiately.

Can some help me what to change to let the map appear and dissapear after I press the button again?,Hi! I am trying to create a code, so the player can take out the map, look at it and after that put it away. I managed to create this:

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

public class mapcontroll : MonoBehaviour

{
    [SerializeField] private bool triggerActive = false;
    [SerializeField] private Animator myanimatorcontroller;

    private void FixedUpdate()
    {
        if (!triggerActive && Input.GetKeyDown("m"))
        {
            myanimatorcontroller.SetBool("mapin", true);
            myanimatorcontroller.SetBool("mapout", false);
            triggerActive = true;
            Debug.Log("trigger active");
        }
    }
    private void Update()
    {
        if (triggerActive && Input.GetKeyDown("m"))
        {
                myanimatorcontroller.SetBool("mapout", true);
                myanimatorcontroller.SetBool("mapin", false);
                triggerActive = false;
                Debug.Log("trigger not active");
        }       
    }
}

When I pressed the key nothing happened. So I added the Debug.Log part, so I can figure out where the problem is.

I found out this way, that when I press the “m” key, both part of the script activate at the same time. So the map starts appearing but also goes back immeadiately.
Can some help me what to change to let the map appear and dissapear after I press the button again?

Hi. Try this (:

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

public class mapcontroll : MonoBehaviour

{
    [SerializeField] private bool mapOpen = false;
    [SerializeField] private Animator myanimatorcontroller;

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.M))
        {
            if (mapOpen)
                CloseMap();
            else
                OpenMap();
        }
    }

    void OpenMap()
    {
        Debug.LogFormat("{0}: Opening map", this.GetType());
        mapOpen = true;
        SetAnimatorValues();
    }

    void CloseMap()
    {
        Debug.LogFormat("{0}: Closing map", this.GetType());
        mapOpen = false;
        SetAnimatorValues();
    }

    void SetAnimatorValues()
    {
        myanimatorcontroller.SetBool("mapout", !mapOpen);
        myanimatorcontroller.SetBool("mapin", mapOpen);
    }
}

Thank you very much for your help! It helped a lot!