📖 Scenario: You are making a simple Unity game where a ball moves based on player input. You want to understand how to use Update and FixedUpdate methods correctly to move the ball smoothly and with proper physics.
🎯 Goal: Build a Unity script that moves a ball using Update for input reading and FixedUpdate for applying physics movement.
📋 What You'll Learn
Create a Rigidbody variable to control the ball's physics
Use Update to read horizontal input into a variable
Use FixedUpdate to apply movement to the Rigidbody
Print messages in both Update and FixedUpdate to see when they run
💡 Why This Matters
🌍 Real World
Games often need smooth and realistic movement. Using <code>Update</code> and <code>FixedUpdate</code> correctly helps make player controls responsive and physics stable.
💼 Career
Understanding Unity's update methods is essential for game developers to create smooth gameplay and avoid bugs related to physics and input timing.
Progress0 / 4 steps
1
Setup Rigidbody variable
Create a public Rigidbody variable called rb inside the BallController class.
Unity
Hint
Use public Rigidbody rb; inside the class.
2
Read input in Update
Add a private float variable called horizontalInput. In the Update method, set horizontalInput to Input.GetAxis("Horizontal"). Also, add a Debug.Log statement inside Update that prints "Update called".
Unity
Hint
Use Input.GetAxis("Horizontal") to read left/right input.
3
Move Rigidbody in FixedUpdate
Add a FixedUpdate method. Inside it, create a Vector3 called movement with horizontalInput as the x value and 0 for y and z. Then call rb.MovePosition(rb.position + movement * 5f * Time.fixedDeltaTime). Also add a Debug.Log statement that prints "FixedUpdate called".
Unity
Hint
Use rb.MovePosition inside FixedUpdate to move the ball smoothly with physics.
4
Print final messages
Add a print statement in Update that prints the current horizontalInput value. Add a print statement in FixedUpdate that prints the current rb.position.
Unity
Hint
Use print with f-strings to show values in console.
Practice
(1/5)
1. What is the main difference between Update and FixedUpdate in Unity?
easy
A. Both run at the same time but handle different components.
B. Update runs every frame; FixedUpdate runs at fixed time intervals.
C. FixedUpdate runs every frame; Update runs at fixed time intervals.
D. Update is only for physics; FixedUpdate is for animations.
Solution
Step 1: Understand the timing of Update
Update is called once per frame, so its timing depends on the frame rate.
Step 2: Understand the timing of FixedUpdate
FixedUpdate is called at fixed intervals, independent of frame rate, ideal for physics calculations.
Final Answer:
Update runs every frame; FixedUpdate runs at fixed time intervals. -> Option B