Slightly more advanced player movement

Today my goal is to get WASD bound to movement of my Player.

So, last I left my Player object, it was sliding endlessly to the side… Though I could control the speed it was sliding, now I want to put the power to move in the fingers of anyone who plays the game.

In Unity, I’m going to go to the Edit menu and select Project Settings, then ‘Input Manager’ to verify the keybinds and the name of the axes I want to use.

I can see Horizontal and Vertical in the Axes menu, which are the options I’m interested in.

Going back to my code, I’m going to assign the horizontal axis to a float value in Update as float HorizontalInput = Input.GetAxis(“Horizontal”); and then slide it into the existing Translate with more multiplications.

This will multiply the Vector3 by the horizontal input, then by speed. If I’m not pressing anything, it will be 0. Left is -1, and right is 1, enabling smooth movement.
Horizontal movement in progress!

Next, I have to do the same for vertical movement! Though I know to be careful and not to simply add in a new float into the existing translate, and also not to just copy and paste the existing lines of code. I want to rewrite the whole line from scratch, to avoid making a mistake and ensure I’m aware of what I’m writing.

I could copy and paste and potentially end up with something like this, which is just using W and S to move me left and right!

I make sure I have transform.Translate(new Vector3(0, 1, 0) * VerticalInput * _speed * Time.deltaTime); which gives me full axes movement within my 2D play space.

Now you might notice that I’m able to move out of the play space, but I’ll be solving that soon.

That’s it for now, but next time I’ll be setting up player boundaries.



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