Welcome to the world of Unity game development! Today, we’re diving into one of the most important skills in 2D game development: making things move. Whether it’s a player-controlled character or an NPC, learning how to control movement is a must. we’ll will explore two approaches: directly moving the transform and using a Rigidbody2D component. In this post we will focus on moving the Transform
directly.
Method 1: Moving the Transform
The transform is like an object’s “position controller” in Unity. By changing its position directly, we can make an object move. This method is straightforward and great for simple movement. There are at least 2 ways to do this, each method has the same effect.
Option 1: Using transform.position
The transform.position
property directly sets an object’s location in the game world. This approach involves recalculating the position every frame and assigning it a new value.
Code Example: Moving Using transform.position
using UnityEngine;
public class MoveTransformPosition : MonoBehaviour
{
[SerializeField] private float moveSpeed = 5f; // How fast the object moves
private void Update()
{
// Get input from the arrow keys or WASD
// Left and Right
float horizontal = Input.GetAxis("Horizontal");
// Up and Down
float vertical = Input.GetAxis("Vertical");
// Get the current position of the object
Vector3 currentPosition = transform.position;
// Calculate the new position
Vector3 newPosition = currentPosition + new Vector3(horizontal, vertical, 0).normalized * (moveSpeed * Time.deltaTime);
// Update the position
transform.position = newPosition;
}
}
What’s Happening Here?
transform.position
: Reads the current position of the object.newPosition
: Calculates a new position based on player input.Time.deltaTime
: Makes the movement frame-rate independent for smooth gameplay.
Pros and Cons of transform.position
- Pros: Clear and explicit control over the position.
- Cons: Can feel repetitive when recalculating the position manually.
Option 2: Using transform.Translate
The transform.Translate
method is a more concise way to move objects. Instead of manually calculating the new position, it handles the addition for you.
Code Example: Moving Using transform.Translate
public class MoveTransformTranslate : MonoBehaviour
{
[SerializeField] private float moveSpeed = 5f; // How fast the object moves
private void Update()
{
// Get input from the arrow keys or WASD
float horizontal = Input.GetAxis("Horizontal"); // Left and Right
float vertical = Input.GetAxis("Vertical"); // Up and Down
// Create a movement vector
Vector3 movement = new Vector3(horizontal, vertical, 0).normalized;
// Move the object
transform.Translate(movement * (moveSpeed * Time.deltaTime));
}
}
What’s Happening Here?
transform.Translate
: Moves the object by adding the movement vector to its current position.movement
: A vector representing the direction and magnitude of movement.
Pros and Cons of transform.Translate
- Pros: Simplifies movement logic; no need to manually calculate a new position.
- Cons: Less explicit control over position adjustments.
Note, in both examples we normalize
the direction of the input. This is so that moving in a diagonal direction is not faster than simple left, right, up, down movement.
One other thing of note is that we use [SerializeField] private moveSpeed
to declare a variable for the movement speed. Why is this?
[SerializeField]
is a C# attribute declared by Unity. I created a post on this attribute and talk about it here. So instead of making a variable that we want to expose to the editor public, we tell Unity to create a copy of this variable and its contents and save it off to disk. The editor can then access the contents but it remains private to the rest of our codebase thus avoiding a lot of potential bugs in our code
Comparing transform.position
and transform.Translate
Feature | transform.position | transform.Translate |
---|---|---|
Control | More explicit; requires manual calculation | Handles calculations automatically |
Ease of Use | Slightly more verbose | Cleaner and more concise |
Recommended Use | Fine-tuning or custom logic needed | Quick, straightforward movement logic |
Which One Should You Use?
- Use
transform.position
if you need explicit control or additional logic for movement. - Use
transform.Translate
for quick and simple movement without extra calculations.
Either way, you’ll have full control over how your object moves in your game! Next, let’s explore how Rigidbody2D introduces physics-based movement.
On to Rigidbody Movement
Next we tackle Rigidbody movement join me for more coding fun here.