Movement is one of the most common things you will need to know more about when it comes to frameworks and engines.
This is the reason I’ve decided to do a small write up of the most common ways on how to move a object.

The most common way of moving objects in Unity is to set transform.position to a Vector2 or a Vector3. This will change the position of the GameObject that holds the component from which the code is called from. But it is also possible to call transform.Translate(vector), which will add the vector the current position. In order to move a object towards another position you can use a several methods that are callable on the Vector3 or Vector2 structs. Such as Vector3.MoveTowards(a,b,distance) or Vector3.Lerp(a,b,t). A example of moving a object to a point would be “transform.position = Vector3.MoveTowards(transform.position, point, Time.Deltatime * speed);”. People also often use libraries to move objects, an example of a good tweening library would be DOTween.

setting transform.position

The quickest way to move a object to a specific position is to set the transform.position field.
This will change the position of the game object that the component is attached to.

Another way of moving the object, opposed to setting it’s position is to call transform.Translate(direction).

using UnityEngine;
public class MoveObject : MonoBehaviour
{
void Start()
{
// Changes the position to x:1, y:1, z:0
transform.position = new Vector3(1, 1, 0);
// It is also possible to set the position with a Vector2
// This automatically sets the Z axis to 0
transform.position = new Vector2(1, 1);
// Moving object on a single axis
Vector3 newPosition = transform.position; // We store the current position
newPosition.y = 100; // We set a axis, in this case the y axis
transform.position = newPosition; // We pass it back
}
private void Update()
{
// We add +1 to the x axis every frame.
// Time.deltaTime is the time it took to complete the last frame
// The result of this is that the object moves one unit on the x axis every second
transform.position += new Vector3(1 * Time.deltaTime, 0, 0);
}
}
view raw MoveObject.cs hosted with ❤ by GitHub

EASY WAY TO SET SINGLE AXIS USING EXTENTION METHODS

In case you want to move a object on a single axis, and don’t want to create a local variable each time you do it. You can use extention methods. Extention methods give extended functionality to existing classes and structs. These are useful particually since some classes are not modifyable within Unity. Such as the Vector3 struct.

When you import the script below, you are able to set the X position of a object to 20 by calling:
transform.SetX(20);

using UnityEngine;
public static class TransformExtentions
{
public static void SetX(this Transform transform, float x)
{
transform.position = new Vector3(x, transform.position.y, transform.position.z);
}
public static void SetY(this Transform transform, float y)
{
transform.position = new Vector3(transform.position.x, y, transform.position.z);
}
public static void SetZ(this Transform transform, float z)
{
transform.position = new Vector3(transform.position.x, transform.position.y, z);
}
}

using Vector3.Movetowards

Sometimes you want an object to move to a specific destination. Vector3.Movetowards is the simplest way of achieving this.
You specify your initial point from which you want to move. And the point which you want to move toward, along with a variable that specifys at what speed it should move.

using UnityEngine;
public class MoveTowardsExample : MonoBehaviour
{
[SerializeField] private Vector3 target = new Vector3(1, 1, 1);
[SerializeField] private float speed = 1;
private void Update()
{
// Moves the object to target position
transform.position = Vector3.MoveTowards(transform.position, target, Time.deltaTime * speed);
}
}

Linear interpolation with Vector3.LERP

How linear interpolation works is that it interpolates between two points. A, B based on a input value (T).
So for example A:0, B:10, and T: 0.5 would return the value of 5.

This is often used in a wrong way by developers, this is because they call Vector3.Lerp(posA,posB, Time.Deltatime).
Which results in a object that eases towards the target, without ever fully reaching it.
Calling it in that way would be the same as calling transform.position += distance* 0.1f; .
Constantly adding a portion of the remaining distance, which will eventually be 0.052131 and then 0.025… etc.

The example given below is the correct way to make a object move from point A to B.
As a addition to this I’ve also included the code on how this function is made. 

using UnityEngine;
public class LerpExample : MonoBehaviour
{
[SerializeField] private Vector3 pointA = new Vector3(-2, 0, 0);
[SerializeField] private Vector3 pointB = new Vector3(2, 0, 0);
[SerializeField] private float speed = 1;
private float t;
private void Update()
{
t += Time.deltaTime * speed;
// Moves the object to target position
transform.position = Vector3.Lerp(pointA, pointB, t);
// Flip the points once it has reached the target
if (t >= 1)
{
var b = pointB;
var a = pointA;
pointA = b;
pointB = a;
t = 0;
}
}
// What Linear interpolation actually looks like in terms of code
private Vector3 CustomLerp(Vector3 a, Vector3 b, float t)
{
return a * (1 - t) + b * t;
}
}
view raw LerpExample.cs hosted with ❤ by GitHub

Using the easing functions script below (Click on view raw to see full script), you can ease the t value.
Which you would write like this: Vector3.Lerp(pointA, pointB, Easing.Quadratic.InOut(t)); This will make the movement of the object very smooth as displayed below.

*/
public delegate float EasingFunction(float k);
public class Easing
{
public static float Linear (float k) {
return k;
}
public class Quadratic
{
public static float In (float k) {
return k*k;
}
public static float Out (float k) {
return k*(2f - k);
}
view raw Easing.cs hosted with ❤ by GitHub

PLUGIN TO MAKE IT EASY: DOTween

Personally when I’m working I don’t like to spend more time then necessary on movement code. Preferably I move a object within a single line of code.
This is why I like to use easing libraries. DoTween is my favourite so far, and I implement it in almost any game I develop for. Including MASKED, Tiny Gems and BOXKILL. It won’t be used in any of the templates I put on the asset store. This is because it adds complexity for the users.

Aside from moving objects it also helps with scale, rotation, fading of canvas groups. And many other things.
So I definitely reccomend it to take a look. They also have a free version of the plugin available with less features.

As an example, I’ve written the same code as above, but then in DOTween. As you can see it requires less code to get the same results.
It also has the benefit that you don’t have to use many Update() loops in your game, which results in less overhead.

using DG.Tweening;
using UnityEngine;
public class DoTweenLerpExample : MonoBehaviour
{
[SerializeField] private Vector3 pointA = new Vector3(-2, 0, 0);
[SerializeField] private Vector3 pointB = new Vector3(2, 0, 0);
[SerializeField] private float speed = 1;
private void Start()
{
transform.position = pointA;
Tweener tweener = transform.DOMove(pointB, speed);
tweener.SetEase(Ease.InOutQuad);
tweener.SetLoops(int.MaxValue, LoopType.Yoyo);
}
}