Project
Beginner Gameplay Scripting
GetButton and GetKey
Tutorial
Beginner
5 Mins
Unity Technologies
Overview
You have completed 0% of this Tutorial.
1.
GetButton and GetKey
Summary
How to get button or key for input and how these axes behave / can be modified with the Input manager
Language
English
Recommended Unity Versions
4
1.
GetButton and GetKey

KeyInput

using UnityEngine; using System.Collections; public class KeyInput : MonoBehaviour { public GUITexture graphic; public Texture2D standard; public Texture2D downgfx; public Texture2D upgfx; public Texture2D heldgfx; void Start() { graphic.texture = standard; } void Update () { bool down = Input.GetKeyDown(KeyCode.Space); bool held = Input.GetKey(KeyCode.Space); bool up = Input.GetKeyUp(KeyCode.Space); if(down) { graphic.texture = downgfx; } else if(held) { graphic.texture = heldgfx; } else if(up) { graphic.texture = upgfx; } else { graphic.texture = standard; } guiText.text = " " + down + "\n " + held + "\n " + up; } }

ButtonInput

using UnityEngine; using System.Collections; public class ButtonInput : MonoBehaviour { public GUITexture graphic; public Texture2D standard; public Texture2D downgfx; public Texture2D upgfx; public Texture2D heldgfx; void Start() { graphic.texture = standard; } void Update () { bool down = Input.GetButtonDown("Jump"); bool held = Input.GetButton("Jump"); bool up = Input.GetButtonUp("Jump"); if(down) { graphic.texture = downgfx; } else if(held) { graphic.texture = heldgfx; } else if(up) { graphic.texture = upgfx; } else { graphic.texture = standard; } guiText.text = " " + down + "\n " + held + "\n " + up; } }

Woohoo! You nailed this tutorial.
Continue rocking your Beginner Gameplay Scripting project.
Next Step:
GetAxis
Tutorial
Beginner
5 Mins
Unity Technologies