Unity Foundations·From empty scene to playable build

C# components and the update loop

A MonoBehaviour is a component; its lifecycle methods are your timing contract.

Beginner 15 min read Lesson 7 of 300 Challenge included
What you will be able to do
  • Cache GetComponent results in Awake.
  • Use Time.deltaTime for frame-rate-independent movement.
  • Do not do physics in Update; use FixedUpdate for Rigidbody forces.

In plain English

Awake runs before Start; Update runs every frame; FixedUpdate is for physics. Keep Update small, cache component references and avoid allocating every frame. Separate input, movement and presentation into components.

PlayerController.csC#
using UnityEngine;

public class PlayerController : MonoBehaviour
{
    [SerializeField] private float speed = 6f;
    private Rigidbody body;
    private Vector3 input;

    private void Awake()
    {
        body = GetComponent<Rigidbody>();
    }

    private void Update()
    {
        input = new Vector3(
            Input.GetAxisRaw("Horizontal"),
            0f,
            Input.GetAxisRaw("Vertical"));
    }

    private void FixedUpdate()
    {
        body.MovePosition(body.position + input.normalized * speed * Time.fixedDeltaTime);
    }
}
Your turn

Move a player

Medium

Cache a Rigidbody and move it with normalized input in FixedUpdate.

CSHARP · starter
void FixedUpdate() {}
Check your understanding before moving on.
Knowledge checkQuestion 1

Which loop is appropriate for Rigidbody physics?

Select an answer to continue
Study tools

Make it yours.

Keep a private note for this lesson, then turn the key ideas into flashcards you can revisit.

Sign in to save notes and flashcards.Both are private to your account and stay with your progress.
Sign in
Finished reading?

Lock in what you just learned.

Mark this lesson complete to save your place and move one step closer to the certificate.

Lesson 7 of 300 · 0% of course