,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?
Answer by andzq · Apr 20, 2021 at 12:47 PM
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);
}
}
To my experience it is not adviced to mix "Update" and "FixedUpdate" with the same type of logic. Keystrokes should usually be checked in Update only whereas FixedUpdate is typically used for altering rigidbodies or other physics related stuff. In your case, using both methods to check for the same key can cause problems. One problem might be a flip/flop effect where "Update" registers the keystroke and opens the map immediatly followed by "FixedUpdate" closing it again. This happens because both methods register the KeyDown-event in the same frame and will execute the logic that is bound to this event.