Day 29 of 100 Days of VR: Spawning New Enemies in Unity and Dealing With “Ghost” Colliders

  1. Start spawning all 3 of our enemies
  2. Fix the shooting bug

Step 1: Spawn our new enemies

  1. 5 Knights at Wave 1
  2. 5 Bandits at Wave 2
  3. 5 Zombie at Wave 3

Step 2: Solving the Mystery Missing Shot

Step 2.1: Disabling our Colliders in Code

using UnityEngine;public class EnemyHealth: MonoBehaviour
{
public float Health = 100;
public AudioClip[] HitSfxClips;
public float HitSoundDelay = 0.5f;
private SpawnManager _spawnManager;
private Animator _animator;
private AudioSource _audioSource;
private float _hitTime;
void Start()
{
_spawnManager = GameObject.FindGameObjectWithTag("SpawnManager").GetComponent<SpawnManager>();
_animator = GetComponent<Animator>();
_hitTime = 0f;
SetupSound();
}
void Update()
{
_hitTime += Time.deltaTime;
}

public void TakeDamage(float damage)
{
if (Health <= 0) { return; } Health -= damage; if (_hitTime > HitSoundDelay)
{
PlayRandomHit();
_hitTime = 0;
}
if (Health <= 0)
{
Death();
}
}
private void SetupSound()
{
_audioSource = gameObject.AddComponent<AudioSource>();
_audioSource.volume = 0.2f;
}
private void PlayRandomHit()
{
int index = Random.Range(0, HitSfxClips.Length);
_audioSource.clip = HitSfxClips[index];
_audioSource.Play();
}
private void Death()
{
_animator.SetTrigger("Death");
_spawnManager.EnemyDefeated();
foreach (Collider collider in GetComponentsInChildren<Collider>())
{
collider.enabled = false;
}
}
}

New Variables Used

Walking Through the Code

Conclusion

--

--

--

Software Engineer by day, side hustler wannabee by night! https://leetdev.io

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Launching Webpage on AWS using EFS Service

Check The Relationships Between Two Lists in Python

Web Scraping Made Easy

What basically Computer is?

Handling missing data in Pandas

Cypress for Ruby on Rails developers

FFMpeg explanation

Unpacking a Raw Packet | Transport Layer Segment | Part -3

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Josh Chang

Josh Chang

Software Engineer by day, side hustler wannabee by night! https://leetdev.io

More from Medium

The Complete Roadmap for C Programming with Covered all topics Basics to Advanced…

100 Days of Code Quickie: Variables

A Step-by-Step Guide to Teaching Yourself Computer Programming — Just Life Trends

Connection between C# and MYSQL