Skip to main content
‹ Back to Tutorials
Intermediate Unity

Learn how to make Games: Unity and Playmaker (Part 4)- Using Key Inputs to Make Things Move. YES!

Sep 30, 2020
About this tutorial

In this Unity and Playmaker tutorial, I'll be working through setting up key inputs to make your player ship move. How to Make a Video Game With Unity3D and Playmaker Series: Part 1: Introduction to Unity3D - https://youtu.be/loCS3NtySaU Part 2: Installing and Using Playmaker - https://youtu.be/t

Written Guide

Part four of the series moves from automatic motion to player control, making the spaceship respond to the keyboard with FSMs in two distinct ways: stepping one unit per key press, and gliding continuously while a key is held. Building both teaches the difference between discrete input events and per-frame velocity.

Listen for key-press steps

Start fresh with a state renamed Listener. From the action browser add Get Key Down (note the three flavors: Get Key fires while held, Get Key Down on press, Get Key Up on release). Add one for the Left arrow sending a new left key event, and a second for the Right arrow sending right key. Delete the default FINISHED transition so the state stays parked, listening, until a key is actually pressed.

Move a step per press

Create Move Left and Move Right states. In Move Left add iTween Move Add on the Owner with a vector of -1 on X (negative is left, positive is right) over about 0.5 seconds with linear easing, and a Finish Event wired back to Listener. Copy it into Move Right and change X to +1. Drag the listener's left key and right key transitions to their states, forming two loops: press a key, move one unit, return to listening. Bumping the value to 3 makes each step travel farther.

Switch to continuous input

For smooth held-key movement, clear the FSM back to a blank Listener. Open Edit > Project Settings > Input Manager and note the built-in Horizontal axis, which already maps the left/right arrows plus A and D. Copy its name to reuse it.

Read the axis into a variable

In the Listener add Get Axis, paste Horizontal as the axis name, leave the multiplier at 1 for now, and store the reading in a new variable x move, updating every frame. Playmaker tells you the variable must be a float — a handy way it guides you to the correct type. The state now continuously captures how far left or right the keys are pushing.

Drive velocity and fix gravity

Add Set Velocity on the Owner; it prompts you to add a Rigidbody, so accept that, then feed x move into the X axis and run every frame. On first test the ship falls off-screen because the Rigidbody has mass and gravity — uncheck Use Gravity since you only want input-driven motion. Now the ship slides left and right as long as a key is held; raise the Get Axis multiplier (e.g. to 5) to speed it up. Two clean techniques: discrete steps via key-down events, and fluid motion via per-frame velocity.

Unity tools by Bliz Studio
Built for our own games — now on the Asset Store

Multi Tag — unlimited hierarchical tags for any GameObject. Hierarchy Pro — style your whole hierarchy with rules. Made by a Unity dev, for Unity devs.

Explore our Unity assets →