Unity Basics
  • Welcome
  • Getting Started with Unity Software
  • Helpful Shortcuts
  • The Unity Software Interface
    • The Default Interface
    • The Windows (Tabs)
      • Hierarchy Window
      • Scene Window
      • Project Window
      • Inspector Window
      • Game Window
      • Console Window
    • Other Layouts
      • Create a Custom Layout
  • Create
    • Creating Game Objects
      • 3D Primitives
      • Creating Game Objects in the Editor
      • Spawning Objects
      • Unhiding/Hiding Objects During Gameplay
      • Parenting
    • Creating Assets
    • Creating Components and Scripts
      • Adding Components in the Inspector
      • Creating Custom Components and Scripts
    • Creating Prefabs
  • Select and Update
    • General Selection
    • Search and Focus
    • Updating Game Objects
      • Updating Game Objects in the Editor
      • Updating Prefabs
    • Updating Assets
    • Updating Components
      • Update Components in the Inspector
      • Accessing Components Through Scripts
  • Delete
    • Deleting Game Objects
      • Deleting and Disabling Objects in the Editor
      • Using the Destroy() Function
    • Deleting Components and Scripts
      • Disabling and Removing Components in the Editor
      • Disabling and Removing Components During Gameplay
  • Translate, Rotate, and Scale
    • Intro to Transforms
    • Handy Transform Shortcuts
    • Translate
      • Positioning Game Objects in the Editor
      • Updating Position Through Script Code
      • Using the Transform.Translate() Function
    • Rotate
      • Rotating Game Objects in the Editor
      • Updating Rotation Through Script Code
      • Using the Transform.Rotate() Function
    • Scale
      • Resizing/Scaling Objects in the Editor
      • Updating Scale Through Script Code
    • Controlling Speed
    • Common Issues with Transforms
  • Materials
    • Material Basics
      • Creating and Applying Materials
      • Accessing Materials Through Code
  • Physics
    • Physics Basics
    • Colliders and Triggers
      • Collider Component Overview
      • Accessing Colliders Through Scripts
      • Common Issues: Colliders and Triggers
    • Rigidbody Component
      • Rigidbody Component Overview
      • Accessing the Rigidbody Component Through Scripts
      • Common Issues: Rigidbody Components
    • Common Issues: Physics and Velocity
  • Interaction
    • Interaction Basics
    • Keyboard Controls
    • Mouse Controls
  • Coding Basics
    • Intro to Scripts
    • Variables
    • Conditionals (If / Then / Else)
    • Arrays
    • Loops
    • Functions
Powered by GitBook
On this page
  • Types of Direct Keyboard Input
  • Using the Input Manager

Was this helpful?

  1. Interaction

Keyboard Controls

Use a script and the Input class to determine what key is pressed on the keyboard:

Types of Direct Keyboard Input

Input.GetKey() - Returns true each frame the key within the parentheses is pressed. This is great for moving objects.

Input.GetKeyDown() - Returns true only on the first frame the key within the parentheses is pressed. This is best for switches and buttons.

Input.GetKeyUp() - Returns true on the frame the key within the parentheses is released.

Inside the parentheses, you want to use the KeyCode class to access the key you want.

Since the code is to see if a key is pressed or not, the code is generally used in if statements.

Here is an example of basic player movement code added into a "PlayerMotor.cs" script attached to a player game object:

using UnityEngine;

public class PlayerMotor : MonoBehaviour
{
    public float moveSpeed = 10;
    public float jumpHeight = 200;
    
    void Update()
    {
        if (Input.GetKey(KeyCode.W))
        {
            transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
        }

        if (Input.GetKey(KeyCode.A))
        {
            transform.Translate(Vector3.left * moveSpeed * Time.deltaTime);
        }

        if (Input.GetKey(KeyCode.S))
        {
            transform.Translate(Vector3.back * moveSpeed * Time.deltaTime);
        }

        if (Input.GetKey(KeyCode.D))
        {
            transform.Translate(Vector3.right * moveSpeed * Time.deltaTime);
        }
        
        if (Input.GetKeyDown(KeyCode.Space)) {
            transform.GetComponent<Rigidbody>().AddForce(Vector3.up * jumpHeight);
        }
    }
}

Common Issue: The object that should continuously move only moves a tiny bit and only when the player continues to tap the key. Solution: You might be using GetKeyDown() which only runs for one frame. Try using GetKey() instead.

Using the Input Manager

If you set up the Input Manager, the following is an example that can be used in place of the code above. It is better to use the Input Manager axes if you plan to have the player be able to change the controls.

using UnityEngine;

public class PlayerMotor : MonoBehaviour
{
    public float moveSpeed = 10;
    public float jumpHeight = 200;
    
    void Update()
    {
        if (Input.GetAxis("Vertical") > 0)
        {
            transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
        }

        if (Input.GetAxis("Horizontal") < 0)
        {
            transform.Translate(Vector3.left * moveSpeed * Time.deltaTime);
        }

        if (Input.GetAxis("Vertical") < 0)
        {
            transform.Translate(Vector3.back * moveSpeed * Time.deltaTime);
        }

        if (Input.GetAxis("Horizontal") > 0)
        {
            transform.Translate(Vector3.right * moveSpeed * Time.deltaTime);
        }

        if (Input.GetButtonDown("Jump")) {
            transform.GetComponent<Rigidbody>().AddForce(Vector3.up * jumpHeight);
        }
    }
}

PreviousInteraction BasicsNextMouse Controls

Last updated 4 years ago

Was this helpful?