16 Detect collissions and Destroy Objects using custom methods

Опубликовано: 20 Март 2026
на канале: Sudheendra S G
51
1

00:00 Session Goal – Auto-spawn animals + destroy on collision
01:21 Open Project – Unity Hub → Deer Game + Patreon starter zip option
02:21 SpawnManager Overview – Current spawn happens with S key
03:13 Create Custom Method – Make SpawnRandomAnimal() and move spawn code into it
04:40 Call Method on Key – Replace spawn code with SpawnRandomAnimal();
05:33 Auto Spawning – Use InvokeRepeating() inside Start()
07:10 Tuning Variables – Add startDelay + spawnInterval as public floats (Inspector controls)
08:33 Test Auto Spawn – Animals spawn every interval without pressing keys
09:10 Add Colliders to Animals – Open prefabs, add Box Collider, adjust bounds, enable Is Trigger
12:35 Projectile Setup – Pizza prefab: Box Collider + Is Trigger + Rigidbody (no gravity)
13:27 DetectCollisions Script – Create script using OnTriggerEnter(Collider other)
15:23 Apply to Prefabs – Add script to animals + Overrides → Apply All
16:31 Gameplay Tweaks – Reduce spawn range (30 → 20), delete scene animals, save
17:03 Fix Pizza Not Destroying – Add Destroy(other.gameObject);
18:20 Game Over Logic – In MoveForward, log "Game Over!" when animal crosses boundary
20:37 Wrap Up – Full loop working: spawn → shoot → collide → game over

Get free resources for this course from the below patreon link

  / 145923169  

Welcome to Session 16 of the Unity 6.3 Junior Programmer Series (Deer Game Project)!
In this session, we upgrade our gameplay prototype by adding two must-have game features: automatic enemy spawning and real collision handling.

✅ What you’ll build in this tutorial:

Animals will spawn automatically every few seconds (no more pressing the S key)

You’ll create your first custom method: SpawnRandomAnimal()

You’ll use Unity’s InvokeRepeating() to spawn animals on a timer

You’ll add Box Colliders and enable Is Trigger on animals and the projectile

You’ll add a Rigidbody to the food projectile (pizza) and disable gravity

You’ll write a collision script using OnTriggerEnter()

When the pizza hits an animal, both objects get destroyed

If an animal reaches the other side without being hit, you’ll trigger a Game Over message using Debug.Log()

🎮 By the end of this session, your Deer Game will feel much more like a real game: unpredictable, time-based spawning, and proper hit detection!

📌 Project Files / Starter Download
If you don’t have the project from the previous session, you can download the starter project from the Patreon link shared in the video description / e-learning page:
16deergamecollisionsstart.zip (approx. 1GB)

🚀 Next Step Ideas (Coming Soon)

Add a real Game Over UI screen

Add score tracking when animals are destroyed

Add sound effects for hits and game over

Increase difficulty over time by reducing spawn interva