Reputation: 545
I'm having a weird problem. I'm trying to make an object move in the direction you press for as long as you hold that button. When I use Input.GetButton(), I get an error that the direction I use ("left," "right," "up," or "down") is not defined, but it is defined. If I use Input.GetKeyDown() instead, everything compiles as usual.
using UnityEngine;
using System.Collections;
public class MovementHandler : MonoBehaviour {
void Update () {
// if (Input.GetButton ("left")) //Does Not work
// Debug.Log ("left");
if (Input.GetKeyDown ("left")) //Works
Debug.Log ("left");
}
}
Upvotes: 1
Views: 2000
Reputation: 3097
Input.GetButton and Input.GetKeyDown take two different inputs.
Input.GetKeyDown checks to see if the key "left" is being pressed. That being said, the documentation says you should be using a KeyCode object as your parameter, like this:
if (Input.GetKeyDown(KeyCode.LeftArrow)) // reads the left arrow key
(See GetKeyDown and KeyCode docs.)
Input.GetButton is expecting to check to see if a Button object with the specified name is being pressed.
if (Input.GetButton("Fire1")) //reads the CTRL key
Upvotes: 1