- Home /
Key not recognized
Hello,
I have a problem with the Input.KeyCode command. I need to set an action on a specific key, (which in my case is "c"), but it doesn't respond to it. The code is not wrong since when I tested it with "d" or "q", which are used for moving the character and thus are naturally recognized by the program, it worked. But when I use a key that is not used by default, like "y" or "c", nothing happens.
here's my code :
using UnityEngine;
using System.Collections;
public class Img10Accroupi : MonoBehaviour {
private GameObject image;
Texture3D texture;
GUITexture guitexture;
public float timer = 0.0f;
public bool imageAffichee ;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnTriggerEnter (Collider other)
{
if (imageAffichee == false){
if(other.tag == "Player" ) {
image = new GameObject("img");
image.AddComponent("GUITexture");
image.guiTexture.texture = Resources.Load("10Accroupi") as Texture;
image.transform.position = new Vector3(0.5f, 0.2f, 0.5f);
image.transform.localScale = new Vector3(0.5F, 0.30F, 0.5F);
imageAffichee = true ;
}
}
if (imageAffichee == true)
{
if (Input.GetKey("c"))
{
timer += Time.deltaTime;
if (timer >= 2.0)
{
Destroy (gameObject);
}
}
}
}
}
for the line "if (Input.GetKey("c"))", I also tried "if (Input.GetKey(KeyCode.C)).
Thank you for your answers !
To help debug this, I would suggest putting a Debug.Log() message at each stage to see where this is falling over. I appreciate you say it works with any other key, but there could be an underlying problem which is bypassed by the fact that d and q are used elsewhere.
I'd suggest one at the start of each IF statement for starters.
@$$anonymous$$ortoc, it i the other way around, GetXXXDown/Up only returns true on the frame you are pressing/releasing while GetXXX returns true as long as you press.
Yup, I had it backwards, already deleted the comment when I went to double check.
Answer by robertbu · Feb 04, 2014 at 05:22 PM
I think you have a logic error here rather than issues with GetKey(). You are getting your key inside OnTriggerEnter(). This function will be called only once for each collision. Assuming a frame rate of 60 fps, that means you will have to have around 120 different collisions with the 'C' key held down before your game object is destroyed. Put a Debug.Log() statement between lines 42 and lines 43. You should see this body of code fire once for each collision. I'm not sure of your game mechanic is here, but you may want OnTriggerStay().
Thanks for these quick answers. But this code works with the keys "z" "q" "s" "d" ins$$anonymous$$d of the "c" so I don't think there's a probleme with the Get$$anonymous$$eyDOWN . I'll try the Debug.Log and hope to find the answer!
The code above use 'Get$$anonymous$$ey()', not 'Get$$anonymous$$eyDown()'. Using Get$$anonymous$$eyDown() would make the problem even worse. There is a potential issue here since I believe the OnTrigger*() callbacks works on fixedTimestep, so it is possible for Get$$anonymous$$eyDown() event to be missed, but that should happen for all keys, not just for the 'c' key.
Indeed Collision is part of the Physics of Unity which is run with the Unity framerate while the Input is taken care by the OS. Your solution is to detect the Input in the Update and set a variable with it. According to Unity docs, Physics is done before the Update. So this below should work as OnTriggerEnter, is called should happen before Update, so your boolean set in the previous frame should work.
bool inputPressed;
void Update()
{
inputPressed = false;
if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.C))inputPressed = true;
}
void OnTriggerEnter(Collider col)
{
if (imageAffichee == true && inputPressed)// Do something
}
Answer by Esthernia · Feb 04, 2014 at 08:42 PM
After testing a few things, adding a public bool to see where the problem was and make some tests again, it finally works!
Thanks a lot for your help !
Here's the final code :
using UnityEngine;
using System.Collections;
public class Img10Accroupi : MonoBehaviour {
private GameObject image;
Texture3D texture;
GUITexture guitexture;
public float timer = 0.0f;
public bool triggerEntree ;
public bool destruction;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (triggerEntree == true)
{
if (Input.GetKey("c"))
{
destruction = true;
}
}
if (destruction == true )
{
timer += Time.deltaTime;
if (timer >= 2.0)
{
Destroy (GameObject.Find("img"));
}
}
}
void OnTriggerEnter (Collider other) {
if (triggerEntree == false){
if(other.tag == "Player" ) {
image = new GameObject("img");
image.AddComponent("GUITexture");
image.guiTexture.texture = Resources.Load("10Accroupi") as Texture;
image.transform.position = new Vector3(0.5f, 0.2f, 0.5f);
image.transform.localScale = new Vector3(0.5F, 0.30F, 0.5F);
triggerEntree = true ;
}
}
}
}
Your answer
Follow this Question
Related Questions
Is it possible to change keyboard input inGame 1 Answer
No keyboard events detected after any key is held. 0 Answers
Key binding screen 0 Answers
Check the latest key pressed? 2 Answers