Recap: Key and Door
Part of the Physics and Gameplay section of Coddy's Unity journey. Lesson 21 of 71.
This recap puts the chapter together: triggers, OnTriggerEnter, and TryGetComponent to make sure it is the player who came in.
Challenge
EasyWrite the key and the door: only the player can use them.
- In
Key.cs'sOnTriggerEnter, whenother.TryGetComponent(out Player player), setplayer.hasKey = true;, logGot the keyand destroy the key. - In
Door.cs, for the player: withhasKey,gate.SetActive(false);and logThe door opens; otherwise logLocked.
Press Play, try the door, fetch the key and try again. Then Check.
Try it yourself
using UnityEngine;
public class Key : MonoBehaviour
{
// The player takes the key; nothing else does
}
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