🎁 Give the #1 gift request of 2024... a ZTM membership gift card! 🎁

How To Move Game Objects (With Physics) In Unity

Luis Ramirez Jr
Luis Ramirez Jr
hero image

Unless you're planning on launching some kind of retro, text-based game resurgence, you're going to need to add movement in your game design.

the oregon trail

Fortunately, Unity offers dozens of features for creating a full-fledged game right out of the box, including a variety of solutions for moving game objects within a scene.

Huzzah!

In this tutorial, we'll explore how to use both the Transform and RigidBody components for moving game objects, as well as look at why you might want to use one solution over the other.

Let's dive in!

Getting started

Before we get into the exact steps of what to do and why, I wanted to also give you the link to check out my repository that ties into this guide.

unity movement repository

This way you'll be able to follow along with this tutorial, and practice using these solutions.

Got it? Alright, let's get to work. As you can see below, I've prepared a basic project that looks something like this:

ZTM Unity movement project overview

We have a basic cylinder object that'll represent the player, while the green shapes are walls for blocking the player's movement. It's not Zelda level graphics but it's good enough for us to understand these concepts and how they work!

There are two primary goals for this tutorial:

  1. Being able to move the player with the WASD keys (the standard movement options on a keyboard for PC games)
  2. Blocking the player's movement when attempting to move past the walls

Now, before we can begin implementing movement, the keyboard strokes need to be captured. The good news is that this is simple enough to do.

In the core directory, you will find an input action that binds the WASD keys.

Add in WASD inputs

If you're unfamiliar with the Input System package in Unity, don't worry, as it's fairly easy to pick up. If you want a helping hand as well as a deep dive into how to use Unity and become a paid Game Developer, check out my Unity for 3D Game Design course.

Anyways, back to the tutorial. The input action is loaded on a game object called the Game Manager, and this object has the Player Input component.

We're going to use this component to handle the actions with events.

Registering Methods with Events

We'll revisit these methods in a moment, but before then, let's look at 2 solutions and when you might use them.

What are RigidBody and Colliders?

When dealing with game physics, you're going to come across two components:

  • Rigid bodies, and
  • Colliders

Both play an integral role and each have their own pros and cons.

Rigid bodies force Unity to recognize that a game object has mass. This means that physics can be applied to these game objects, and then we can move them around or collide them with other game objects.

For example

If we wanted to create a game of Pool, we could apply this to the white ball so it moves when the cue hits it.

collision

We don't want to only play pool with the white ball though. We want to be able to pot the other balls, and that's where Colliders come in.

Colliders are designed for detecting collisions with other game objects. You can also use this feature to help you perform additional actions, other than physics, during these events.

Colliders also carry some similar characteristics to rigid bodies. If an object has a collider, other objects will not be able to move through the object, even if it doesn't have a rigid body.

If we use the previous example again, then when we hit the white ball, it hits the other balls, and they move around the table. Simple!

So let's have a look at how we might apply this

In the Unity project I've provided, the floors and walls have collider components while only the player has a rigid body. This is because we don't need to move the floor or walls, only the player.

The configuration settings for these components are set to their default values other than the constraints. This is to prevent the game object from rolling around.

RigidBody Constraints

On the Player game object, two custom components are available called:

  • TransformMovement, and
  • PhysicsMovement

Let's go through the code in both of them.

Moving Game Objects with the Transform Component

In the scripts/TransformMovement.cs file, you'll find the logic for the component. Let's focus on this portion first.

private Vector3 movementVector;

public void HandleMovement(InputAction.CallbackContext context)
{
    Vector2 input = context.ReadValue<Vector2>();
    movementVector = new Vector3(input.x, 0, input.y);
}

The HandleMovement method will run whenever the user presses any of the WASD keys.

In this example, we read the input by calling the ReadValue() method. It'll return a Vector2, which is converted into a Vector3 since we're dealing with 3D objects.

This information is stored in a property called movementVector.

Important: Storing the vector outside the method is crucial as we'll need it in another part of our code.

[SerializeField] private int speed = 8;

private void Update()
{
    transform.Translate(movementVector * Time.deltaTime * speed);
}

In the Update() method, we're constantly moving the game object with the Transform component's Translate() method. This then accepts the direction to move the game object.

For framerate independence, we're multiplying the vector with the Time.deltaTime variable.

Can you spot the issue here?

Using the Transform component to move game objects may seem like the obvious choice, but there's one massive pitfall with it.

Moving with Translate

If you were to attempt to move the game object, everything would go smoothly until you start interacting with other game objects.

This is because the Transform component ignores physics when interacting with other objects.

After the object has been moved with the Transform component, physics is applied, which causes the jittery behavior when pushing against the walls.

This isn't always an issue

Using the transform component to move objects is a great option when you don't need to worry about how an object interacts with the world.

However, if you want to be able to stop objects from moving through obstacles, using physics is your best option.

Applying force with Physics

The rigid body component contains methods for moving game objects, and unlike the transform component, it'll adhere to the laws of physics in our game.

First, we need to grab a reference to the component with the GetComponent method. You'll find the following code in the PhysicsMovement.cs file.

private Rigidbody rb;

private void Start()
{
    rb = GetComponent<Rigidbody>();
}

Next, we can start moving the object with the AddForce method.

private void FixedUpdate()
{
    rb.AddForce(movementVector * speed);
}

Just like before, we're passing in a vector. However, there's one huge difference worth noting, and that's the fact that multiplying the vector with time is not necessary.

Why? Well, it's all thanks to the FixedUpdate lifecycle functions.

Let me explain.

If we look at the rigid body recommendations in the Unity documentation, it states that using Update may cause our objects to move inconsistently.

unity documentation

But because we're applying movement in the FixedUpdate method instead, while also using the Time.deltaTime, it means that this variable is redundant.

Alright back to the tutorial.

Before testing the code, you should update the event to use the PhysicsMovement.HandleMovement method, like so:

Registering the method with the Movement event

Here's what the final product looks like.

Finished unity movement project

Unlike before, there's no jittering or movement through the walls. If the player collides with a wall, they're unable to move past it.

Key Takeaways

If you plan on moving objects, you should always use the correct component methods.

  • By using physics, it assures you that objects can interact with each other without causing funky behavior
  • Using the transform component to move objects may make sense at first, but it can cause awkward movements and potentially break your game, so try to think how they will interact

Hopefully this tutorial has helped you get a better grasp on how to use these 2 solutions. Try and set it up for yourself and have a play around!

Psst...

Want to know something cool?

Although these methods both work, there's actually another way of moving game objects, which is through the use of AI.

It's too much to cover right here, but if you want to learn exactly how to move game objects with AI, check out my Unity 3D Game Development course on ZTM. I walk you through an alternative and more predictable solution for traversing objects across a large map, as well as how to build a complete RPG with Unity!

Build an RPG with Unity

More from Zero To Mastery

How To Get Started In Game Design preview
How To Get Started In Game Design

Want to go from playing games to getting paid $100K+ to build them? Our Game Design expert shares the steps to go from total noob to hired as a Game Designer.

[Full Guide] Learn To Code For Free in 2024 & Get Hired in 5 Months (+ Have Fun Along The Way!) preview
Popular
[Full Guide] Learn To Code For Free in 2024 & Get Hired in 5 Months (+ Have Fun Along The Way!)

Updated for 2024 (including A.I. & ChatGPT). In 2014, I taught myself how to code & got hired in 5 months. This is the step-by-step guide I used. Now 1,000s of other people have also used it to learn to code for free & get hired as web developers.

The 6 Mistakes I Made When Learning To Code (And How To Get Past Them) preview
Popular
The 6 Mistakes I Made When Learning To Code (And How To Get Past Them)

This article will help you save time and avoid the mistakes that a lot of beginners make. If I could go back in time, this is how I would have saved myself from countless hours, days, and months working on the wrong things.