In unity there are two basic method of moving an object. Using translate on the object itself, or using a rigidbody.
Moving with translate is easy. All you need is to grab the transform (object) and apply a translate which includes a vector 2 for direction, then speed and time.
However, the issue with translate is that it’ll ignore every physic and collision detection for the object. Which means the character will ignore any other object in the scene. However this is really good for adding or showing any effects that doesn’t interact with the world.
There are three method of moving with RigidBody, use the function AddForce(), set the velocity, or the function MovePosition(). The setup is simple, add a RigidBody, create a variable for speed, and make a Vector2 for movement.
The function AddForce() is simple, give it a direction and a speed variable and it’ll apply a force towards the direction at a constant rate. The longer the button is hold down, the more force the object will receive. The object will then slow down depending on the gravity and friction.
Setting the velocity is similar to adding force, all you need is direction and speed. However setting velocity this way will override all physic elements including gravity, and there will be no acceleration.
The function MovePosition() requires you getting the current position of the object, then add the new direction with the speed and time into it. Since I have been using Vector2 for movement, and the current position of the object is a Vector3, I’ll need to forcefully change the variable to a Vector2 instead. This step is unneeded if you are using a Vector3 for movement.