Geek Culture
Published in

Geek Culture

Setting Up the Legacy Input System in Unity

While Unity does have a new input system for user controls, there is nothing wrong with using their old input system, especially when prototyping.

First, head over to project settings to get started. Scroll down to Input Manager to see some additional settings. In order to set the input properly, we will need to type “Horizontal” exactly as it’s seen here in our code, to access it.

The Unity documentation is very helpful when it comes to seeking information, so never be hesitant to reference it! This next image directly from the Unity documentation tells us exactly what we need to know. Our input will be a variable of type float, being we will need to move smoothly while constantly updating the player’s position. Trying to move in something like whole number increments (int) would yield an undesirable choppy look. The Unity doc then gives us the unique name to call our variable; horizontalInput. The next step is a hierarchical reference to our Input manager shown in the above image, using dot notation for navigation. The word Input, will access our Input Manager, while the dot notation (.) acts as a drop down arrow would when moving through files in windows. The keyword GetAxis is then used to access the axis we are after, and in this case we will enter Horizontal, just as it was shown above in our settings window, only in quotations like shown in the Unity doc example.

In the void Update method, I use the above direction for adding both my horizontal and vertical input axes. I then use the transform of the player and tell it to translate right and up respectively, per axis using a Vector3. That Vector3 is then multiplied by the axis variables we just created, as well as the speed variable and real time. Don’t worry about the right and up directions moving the player once it is looking for an input response. At this point those are just giving a direction of movement. Pressing the left arrow will technically make the direction negative right, to move left, for as long as the arrow key is pressed.

As of right now, the input controls should be fully functional, but let’s clean this up a little. Move these commands to one line of code by using a new Vector3, which takes overloads, and assigning the horizontal and vertical input variables there. I also need to include a zero for the Z axis, being I need to acknowledge it, but this is a 2D game, so I want to keep that value at zero.

To clean things up even more, I create a local variable for my directional Vector3 and call it _direction. Make the value of the Vector3 variable, the new Vector3 with the assigned input variables. Then I can just enter the _direction variable after transform.Translate.

I want to make things more efficient, so I create a method called Player Movement, and then I move everything in there. This way I can simply call the Player Movement method from within void Update.

Without going into too much detail, the movement will still be not quite right. Moving in any one direction will move the player at the desired speed, but moving diagonally will actually give us a little faster movement than the assigned speed value. Fixing the movement speed of multiple vector3’s (up and right for example), can be achieved using the normalized keyword. This is to ensure that diagonal movement is the same speed as single direction movement. At the end of my local Vector3 variable declaration, I fix this by adding .normalized, after the zero reference for the Z axis. Also to note, is changing the GetAxis on both vertical and horizontal inputs, to GetAxisRaw.

Below you can see my player cruising around the screen ,courtesy of the legacy user input system!

--

--

--

A new tech publication by Start it up (https://medium.com/swlh).

Recommended from Medium

RedisInsight

How to implement Chrome style for select tag in all browsers

How to trigger Jenkins Job with Bash Script?

Gorm Vs. Xorm (part 1)

Dashboards in Your Browser: Lessons From Working With d3.js

Generating Notifications for your Jenkins Pipeline

A library to monitor above-the-fold content during rendering

Working with External Hive Metastore on Analytics Engine powered by Apache Spark

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Jared Amlin

Jared Amlin

I am an artist and musician, that is currently diving headfirst into game development with C# and Unity3D.

More from Medium

Using Decals in Unity’s HDRP

How to the Layered Lit shader to mix Texture Maps in Unity HDRP

2.5D Platformer: Wall Jumping

Spicing up our enemy movement