- Home /
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. $$anonymous$$ake sure that you attached the script to the right GO and no other script modifies the position. Also check your speed variable. $$anonymous$$aybe 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 Get$$anonymous$$ey for my $$anonymous$$ac OS X Leon - Input.characterinput is ok, but Input.Get$$anonymous$$ey() - not working with keyboard symbols, only with numbers. WASD also not working for FPS controller. I'm mad about it.
Answer by MacMac098 · Jun 05, 2011 at 01:59 PM
In JavaScript
var moveSpeed : float = 10;
function Update ()
{
if((Input.GetKey("z"))
{
transform.Translate (moveSpeed, 0, 0);
}
}
Answer by frederikedel · Jul 18, 2020 at 01:02 PM
@ 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.
$$anonymous$$aybe thats also possible but apparently its not working. "z" works perfectly fine.
Answer by Yamilla · Jul 28, 2013 at 03:46 PM
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).
Answer by BreadMachine · Jul 18, 2020 at 09:54 AM
(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 ();
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612095500im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Play Animation With GetKey 1 Answer
How to keep object rotating when key is pressed once 2 Answers
Input.GetKey not working 1 Answer
Converting a string to keycode 3 Answers
Stop animation after .5 seconds? 2 Answers