Methods for Moving Unity Characters and Their Differences

Methods for Moving Unity Characters and Their Differences

All Unity Character Movement Systems Methods

 Hello friends, in this article I will tell you about the commands that allow you to move characters and objects in Unity.

I will also try to explain a few small details, such as which command should be used in what situations, happy reading.

Input Manager

Unity Input Manager

The Input Manager is a detection system that gives us user inputs. Instead of writing code individually for devices such as keyboard or game controller to detect them, using the Input Manager allows us to detect them more efficiently.

Input Manager is automatically included in every project, but if you want to make changes to it, you can customize it as you wish from the Edit > Project Settings > Input Manager section within Unity.

Understanding Directions

First, before getting to all the commands, I will explain how we can determine the direction from the user using two different methods.

1) Checking Keyboard Keys

This is one of the methods I do not recommend at all, and after detecting the pressed key from the keyboard using if conditions, the desired operation/command is executed.

        if(Input.GetKey(KeyCode.A)){
command
}
if(Input.GetKey(KeyCode.D)){
command
}
if(Input.GetKey(KeyCode.W)){
command
}
if(Input.GetKey(KeyCode.S)){
command
}

Since it is clear which direction will be taken when which key is pressed with the above command, we can immediately write the direction codes after the if part, and our character moves in that direction.

float speed = 10;
if(Input.GetKey(KeyCode.A)){
transform.Translate(speed * Time.deltaTime,0,0);
}
if(Input.GetKey(KeyCode.D)){
transform.Translate(-speed * Time.deltaTime,0,0);
}
if(Input.GetKey(KeyCode.W)){
transform.Translate(0,0,speed* Time.deltaTime);

}
if(Input.GetKey(KeyCode.S)){
transform.Translate(0,0,-speed* Time.deltaTime);

}

I think the process above contains a bit too many unnecessary lines of code, so I consider this method an unnecessary way.

2) Using Input Manager

This is a truly useful method and with relatively few lines of code, we can get very efficient results.

A Vector3 variable is defined and the data from the Input Manager is placed inside. If Input Manager detects a predefined key is pressed, it gives values like +1 or -1 to represent direction, and you just have to multiply this value by the speed to make your character move.

 Vector3 moveDirections;
moveDirections = new Vector3(Input.GetAxis("Horizontal"), 0 , Input.GetAxis("Vertical"));
transform.Translate(moveDirections.x * speed * Time.deltaTime, 0, moveDirections.z * speed * Time.deltaTime);

I defined a variable called speed as public, and later I can change this speed in my scene to find the appropriate speed value for my character.

According to the key information from the Input Manager, the x and z values of the Vector3 variable change to +1 or -1, and when you multiply it by speed, you get movement.

Horizontal represents the horizontal (right and left arrow keys, a and d keys) direction. 

Vertical represents the vertical (up and down arrow keys, w and s keys) direction.

Methods for Moving

I will explain the movement methods below using the Input Manager methods to determine the movement direction.
For the AddForce and Velocity methods, your character must have a "Rigidbody" component added.

Moving with the Translate Method

This method allows your character to progress by adding to its current position.
The movement speed is determined as a vector3 and the function is called.

 Vector3 moveDirections;
moveDirections = new Vector3(Input.GetAxis("Horizontal"), 0 , Input.GetAxis("Vertical"));
transform.Translate(moveDirections.x * speed * Time.deltaTime, 0, moveDirections.z * speed * Time.deltaTime);

Since this method constantly updates the position, you may encounter problems such as objects passing through each other. Therefore, it is best to use this with objects that have constant movement and do not interact with other objects. If you have to use this method, you need to check collision situations with other objects.

Moving Using Position

This is the same method as Translate; the only difference is you also have to add the current position of the character yourself.
  Vector3 moveDirections;
moveDirections = new Vector3(Input.GetAxis("Horizontal"), 0 , Input.GetAxis("Vertical"));
transform.position = new Vector3(transform.position.x + moveDirections.x, transform.position.y , transform.position.z + moveDirections.z);

The same problems as with Translate are present in this method.

Moving Using AddForce

You can think of this method as constantly adding a torque to the character. It can be used to move the character but is generally used in collision situations.
You can think of this method as similar to the motion that occurs when you hit a ball—at first, it speeds up, then slowly loses speed.
Of course, if you think you are constantly hitting the ball (holding down the key), after a while, the ball will go wild and be hard to control.

  Vector3 moveDirections;
moveDirections = new Vector3(Input.GetAxis("Horizontal") * speed * Time.deltaTime, 0 , Input.GetAxis("Vertical") * speed * Time.deltaTime);
characterRB.AddForce(moveDirections, ForceMode.Impulse);

In general, you do not encounter problems like passing through objects with this method, but if you constantly apply the AddForce method to a character that is next to a wall, you can be sure that at some point the character will pass through the wall.

By the way, there are 4 types of ForceMode for AddForce; if I need to explain them to you;

Force : Constant force is applied using the object's mass.
Acceleration : Constant force is applied regardless of the object's mass.
Impulse : An instant force is applied considering the object's mass.
VelocityChange : An instant force is applied regardless of the object's mass.

Moving Using Velocity

In general, Velocity is the method we use most to make characters jump and move in games.
The character moves as desired and generally does not pass through another object upon encountering it, though rare exceptions exist.
If you keep the movement speed too high, even with Velocity it is possible to pass through objects.

        Vector3 moveDirections;
moveDirections = new Vector3(Input.GetAxis("Horizontal") * speed, 0 , Input.GetAxis("Vertical") * speed);
characterRB.velocity = moveDirections;

In general, we use Velocity to move our character.

Summary

Now you may be asking which of these many operations to use for what and you may feel confused, so let me clarify these a bit for you.

The Translate and position methods are generally used to move static and non-character objects.
AddForce is generally used for objects thrown as a result of a collision, or for objects that start with an instantaneous movement speed such as arrows or bullets and then slow down as they move.
Velocity is generally used for objects controlled by users.