GetKey is not working...

Hi Guyz…
I am new to unity3d.
I have simple question
In following code GetKey is not at all working…

void Update () 
{
	if(Input.GetKey (KeyCode.Z))
	transform.Translate (speed, 0, 0);
     }

Do I need to register ‘Z’ key somewhere in setting or am I missing something???

-pax

No, that should work. Make sure that you attached the script to the right GO and no other script modifies the position. Also check your speed variable. Maybe your problem is that you didn't focus the game window/tab. Try to click once into the window after entering run-mode.

For me it's not working. Defenitly some problem in GetKey for my Mac OS X Leon - Input.characterinput is ok, but Input.GetKey() - not working with keyboard symbols, only with numbers. WASD also not working for FPS controller. I'm mad about it.

5 Answers

5

try this instead:

if(Input.GetKey("z"))

Did you define “speed” before the “Update” loop?
Did you put the Script on the right GameObject?

There are 2 things you can do. Either you can drag the Script ON the Object you want to Translate, or you add something to it. If you take the second option, do it like this:

using UnityEngine;
using System.Collections;

public class Translate : MonoBehaviour {
    public GameObject whatever;
    public float speed = 10.0f;

    void Update() {
        WiiUGamePad Winp = WiiUInputGamePad();

        if (Winp.GetButtonDown(WiiUGamePadButton.ButtonA)) {
            whatever.transform.Translate(speed,0,0);
        }
    }
}

I hope you won’t get confused about the the Wii U stuff, but I’ve typed it over from when I was trying to figure out the Wii U Input System (and this one is correct, by the way).

(Input.GetKeyDown(KeyCode.Z)){

make sure to have { and at the end and close it }

Brackets aren't necessary if the conditional only affects one line. These three if-statements are identical; you only need surrounding braces when multiple lines are affected by the condition. if (condition) DoSomething (); if (condition) DoSomething (); if (condition) { DoSomething (); }

In JavaScript

var moveSpeed : float = 10;

function Update () 

{
if((Input.GetKey("z"))
 {
 transform.Translate (moveSpeed, 0, 0);
 }
}

@ Shrimpy’s Answer is completely right: The letter buttons on your keyboard you get with quotation marks “z”. If you use other Input devices, you have to register the buttons in the Input Manager, try searching for a tutorial if you dont understand it.

> Z is not a Keycode What is this then?? https://docs.unity3d.com/ScriptReference/KeyCode.Z.html