Input and player control
Read player input and move a Rigidbody in the right update loop.
- Read horizontal and vertical axes each frame.
- Move a Rigidbody in FixedUpdate, not Update.
- Normalise the direction so diagonal movement is not faster.
In plain English
The Input System is Unity's newer input package, while the legacy Input Manager is the older built-in approach that still works through methods like GetAxis. Reading axes returns a value from -1 to 1, such as left and right on the horizontal axis. Update runs every rendered frame and is the place to read input, while FixedUpdate runs at a steady physics step and is the place to apply forces to a Rigidbody. A Rigidbody is the component that lets physics move an object, so change its position with physics methods rather than the Transform.
Step by step
- 1Create a GameObject with a Rigidbody and a collider.
- 2Add a PlayerController component to the same object.
- 3Read the horizontal and vertical axes.
- 4Build a direction vector and normalise it.
- 5Move the Rigidbody with MovePosition in FixedUpdate.
using UnityEngine;
public class PlayerController : MonoBehaviour
{
[SerializeField] private float speed = 6f;
private Rigidbody body;
private void Awake()
{
body = GetComponent<Rigidbody>();
}
private void FixedUpdate()
{
float horizontal = Input.GetAxisRaw("Horizontal");
float vertical = Input.GetAxisRaw("Vertical");
Vector3 direction = new Vector3(horizontal, 0f, vertical).normalized;
body.MovePosition(body.position + direction * speed * Time.fixedDeltaTime);
}
}Common mistakes
- Reading input inside FixedUpdate misses rapid presses, because physics runs at a fixed step rather than every frame.
- Moving a Rigidbody by setting its Transform position bypasses physics and can cause tunnelling or jitter.
- Forgetting to add a Rigidbody means GetComponent returns null and the script throws at runtime.
- Skipping the normalise step makes diagonal movement faster than straight movement.
Move a player
Fill in FixedUpdate to read input and move the Rigidbody with MovePosition.
using UnityEngine;
public class PlayerController : MonoBehaviour
{
[SerializeField] private float speed = 6f;
private Rigidbody body;
private void Awake()
{
body = GetComponent<Rigidbody>();
}
private void FixedUpdate()
{
}
}Which method should apply movement to a Rigidbody?
Make it yours.
Keep a private note for this lesson, then turn the key ideas into flashcards you can revisit.
Lock in what you just learned.
Mark this lesson complete to save your place and move one step closer to the certificate.