Crates Keep the Rest
Part of the Physics and Gameplay section of Coddy's Unity journey. Lesson 18 of 71.
Challenge
EasyLet a crate keep the ammo that did not fit.
- In
AmmoCrate.cs, replace theAddAmmoline withamount -= blaster.AddAmmo(amount);. - Call
Destroy(gameObject);only whenamount == 0.
Press Play and walk right: Crate2 stays, holding the 2 that did not fit. Then Check.
Try it yourself
using UnityEngine;
public class AmmoCrate : MonoBehaviour
{
public int amount = 3;
void OnTriggerEnter(Collider other)
{
if (other.TryGetComponent(out Blaster blaster))
{
blaster.AddAmmo(amount);
Destroy(gameObject);
}
}
}
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