Beat the Clock
Part of the Physics and Gameplay section of Coddy's Unity journey. Lesson 32 of 71.
Challenge
MediumAdd a clock to Coin Rush.
- In
Ball.cs, addpublic float timeLimit = 60f;andpublic float timeLeft;; settimeLeft = timeLimit;inStart(). - In a new
Update(), whiletimeLeft > 0f, takeTime.deltaTimeoff it; at 0, set it to0fand log"Time's up! Score: " + score. - In
FixedUpdate(), push only whiletimeLeft > 0f.
Press Play and watch the clock. Then Check.
Try it yourself
using UnityEngine;
public class Ball : MonoBehaviour
{
public float force = 10f;
public int score;
private Rigidbody rb;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void FixedUpdate()
{
float x = Input.GetAxis("Horizontal");
float z = Input.GetAxis("Vertical");
rb.AddForce(new Vector3(x, 0f, z) * force);
}
public void AddScore(int points)
{
score += points;
Debug.Log("Score: " + score);
}
}
All lessons in Physics and Gameplay
1Rigidbody
Gravity and MassPush with AddForceJump with an ImpulseFixedUpdate for PhysicsVelocityKinematic BodiesRecap: Launch Pad4Coroutines
InvokeInvokeRepeating, CancelInvokeYour First CoroutineLoops in CoroutinesStopping CoroutinesRecap: Countdown10Project: Arena Escape
The ArenaHunters2Collisions
OnCollisionEnterWhat Did I Hit?Break on ImpactStanding on the GroundBounce and FrictionCollider ShapesRecap: Breakable Wall5Project: Coin Rush
The Rolling BallCoins to CollectPractice on your own: Unity playground