Implementing Player Movement In Unity
For the purposes of this article, I am going to assume you are familiar with the basics of the Unity Editor. So I won’t be explaining how to place a Game Object or set up a scene, etc... I will be showing you how to move your Player with basic controls in Unity.
Now, once your player object is ready to go, create your C# class that you’ll use for controlling its movement. You should need to set just one float variable, Speed. I usually make this a Serialized Field so I can adjust through the inspector.
Next up, you’ll want to make a method called ‘Move’ or something similar. This will be called from the Update function rather than writing all the code there.
The screenshot above shows that I’ve created local variables to hold a reference to the Input Axis. They are usually named “Horizontal” and “Vertical” but I went into the project settings and changed their labels. My demonstration uses more of a 3rd person view, so the default labels didn’t really apply. The functionality is the same. When I press ‘W’ the vertical value is 1.0f, and -1.0f when I press ‘S’. The horizontal value is 1.0f when I press ‘D’ and -1.0f when I press ‘S’.
With the local variables assigned, I created another Vector3 called ‘direction’ that combines both input axis. I then use ‘ transform.Translate’ method to change the position of my player. This changes the position of the object based on the Vector3 you pass in, which is my ‘direction’ variable, then multiplies that by the Speed variable and then by Time.deltaTime for framerate independence. So now we just call the Move() method in Update() and we should be good to go.
Implementing Player Movement In Unreal Engine 5
As I mentioned above, for the sake of this article I will assume that you’re familiar with the basics of the Unreal Editor.
I should also say that I am a novice in Unreal, so there are likely better, more efficient ways to go about what I am doing. However, my implementation works and can be refactored later.
First, in the Unreal Editor, you’ll need to create a new C++ class derived from the ‘Pawn’ class. This will allow the class to inherit the necessary functionality needed to implement movement.
Declare the public method SetupPlayerInputController in the header file of the Actor class you want to move.
You’re also going to want a Speed variable that you can change in the details panel. So you’ll need to make it a UPROPERTY and then set the access to your preference.
Then, in the C++ file, we’ll implement the method as shown below.
If you’re not using one of the Unreal Engine project templates, you may need to set the input binding names first. Once that’s done, create the methods for moving, one for the X and one for the Y. There may be a better way to do this, but I am showing what works for me.
With all that set, you can compile your code in the Unreal Editor. Don’t forget to set your Speed variable if you didn’t give it a default value! Once the main code was written I created a Blueprint class derived from the C++ class I just covered. Similar to Unity’s prefabs, the Blueprints allow for customization with the Unreal Editor itself.
You might’ve noticed how similar the implementations are in each engine. Each engine has its own advantages and disadvantages, but both can allow you to make amazing games if you’re willing to put in the effort. The purpose of this article is to show a comparison.
The next post will cover firing a projectile in both Unity and Unreal.