Recap: Breakable Wall
Part of the Physics and Gameplay section of Coddy's Unity journey. Lesson 14 of 71.
This recap puts the chapter together: OnCollisionEnter, the tag of what hit you, and the impact speed from collision.relativeVelocity.
Challenge
EasyWrite Brick.cs: a fast cannonball breaks the brick it hits.
- In the brick's
OnCollisionEnter, testcollision.gameObject.CompareTag("Ball")andcollision.relativeVelocity.magnitude >= breakSpeedtogether, with&&. - When both are true, log
Brick brokenandDestroy(gameObject);. Slow balls and falling bricks break nothing.
Press Play, aim with the arrows and fire with Space. Then Check.
Try it yourself
using UnityEngine;
public class Brick : MonoBehaviour
{
public float breakSpeed = 6f;
}
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
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 WallPractice on your own: Unity playground