• entries
44
19
• views
2315

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

875 views

Welcome back to day 29!

Yesterday we finished creating our final enemy that we’re going to use for the game.

Now that we have everything ready, it’s time to use them!

After finishing the spawn system and played the game for a bit, I noticed a small problem.

Sometimes when we shoot, the enemy doesn’t get hit. This becomes obvious when we try to switch the health of our enemy to be 1.

Our goals today are:

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

Without any delays, let’s get started!

Step 1: Spawn our new enemies

Setting up the spawning system with our new enemies is simple, we already have the SpawnManager script to take care of spawning enemies every wave.

In our hierarchy, look at GameManager > SpawnManager and to find our SpawnManager script.

Set our Waves size to be 3.

We want to spawn:

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

When we’re done, we’ll have something like this:

Now we can play the game and we’ll be able to fight all of our new enemies on the map!

Here might be something we’ll see!

You might notice that after we win, our gun doesn’t stop shooting and our shooting sound doesn’t stop.

Not today, but some other day, we will go in and disable all of those when the player wins.

Step 2: Solving the Mystery Missing Shot

Now that we’re spawning enemies and playing the game, we might encounter a very weird problem.

When we’re shooting an enemy, sometimes they don’t get hit.

I’m not talking about a long distance either, I’m talking very close proximity. What’s happening?

After digging around for a bit, I found the problem: our raycast is still hitting the mesh collider of our dead enemies.

As we have discussed in Day 20 the Mesh Colliders that we attach to our enemy doesn’t follow the form of our enemies. They’re just still there, as you can see from the image above.

What’s happening here is that even though we defeated our enemies, their mesh collider is blocking our shots from hitting other existing enemies.

But it’s okay, we can fix this. We must disable all colliders attached to the enemy when they die.

Step 2.1: Disabling our Colliders in Code

Now, let’s think, where would we know when the enemy is dead? In the EnemyAttack script!

In EnemyAttack we have a function called Death() that takes care of everything when the enemy dies.

Let’s use that:


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.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;
}
}
}


None

Walking Through the Code

There’s only one major change that we did and that’s in Death() where search through all of the enemy’s child game objects looking for any collider component. If a collider is found, we would just disable it.

One interesting thing to note is that we’re looking for anything of type Collider. We’re more used to finding something of exact type, for example, GetComponent<SpawnManager>();

However, taking advantage of the fact that all of the Colliders: Box Collider, Mesh Collider, Capsule Collider, etc all extend from the Collider parent class, they’re all considered of type Collider. What that means is that we can do what we did in Death() and look for anything that is of type Collider or inherits the class.

Now after we added the code, if we play the game, we’ll see that dead enemies no longer have their Colliders on anymore:

There are things that are sometimes better off staying dead.

Conclusion

That’s it for today! Today, we incorporated the rest of our enemies to the game and we even went in to fix “ghost” mesh colliders that prevented us from shooting properly.

I think we’re going to work on one more feature before I start moving on to the actual VR portion of this series, and that’s making a point system!

In our current game, we win and that’s it. There’s no replay value at all!

Tomorrow we’re going to start making this game more interesting by adding a scoring system!

With that all being said, it’s getting late, I’m headed out!

Source: Day 29

Visit the 100 Days of Unity VR Development main page.

To get your mesh collision to deform in Unity you will have to either destroy and re-create it each frame, or you will have to replace the Unity collision system with your own.

The problem you have is that the mesh collider uses the original mesh and stays that way. So even when you animate the collider will always look like that.

The slow update of recreating the collisions at run time is why we re-create the collisions using primitives like I showed you. That way you can parent each part to a bone and it will deform with the character; solving this little problem you had.

Then again, games is about solving problems and you did solve this one.

The spawn manager is well made, liked that a lot.

That's really good to know. Thank you as always for your knowledge. It's always a good feeling knowing that there's someone who knows more, such as yourself offering me better ways of implementing things!

Create an account

Register a new account

• Similar Content

• By GameTop
Dirt Bike Extreme - another game made with Unity. Took about 2 months to complete.
Take part in extreme motorcycle races across the dangerous and challenging tracks. Dirt Bike Extreme is easy to pick up but hard to master. Race, jump and crash your way and other mad rivals through the amazing tracks as you master the skills and physics of motocross in this high-speed racing adventure. Conquer challenging routes on 23 different runs, discover new bikes and become the best of the best! Over 257K downloads already!
Windows Version:

Mac Version:
https://www.macstop.com/games/dirt-bike-extreme/

• Continuing to work on “Eldest Souls” (first article here!), I’ve begun familiarising myself with the workflow between Fmod and Unity, and the integration system. I know much of this will be pretty obvious to most, but I thought I’d share my thoughts as a complete beginner learning the ropes of sound designing.
The library of sounds that Fmod provides has been very useful, at least as reference points. I’ve still kept to my ethos of producing the sounds myself as much as possible. Having said that, Fmod gives you 50 free sounds with your download, and I’ve used a wooden crate smash, a drawbridge and electricity sound you can hear in the foley video below.

The thing i found most useful was witnessing changes i made in Fmod being realised instantly in Unity. If a volume needed changing, or the timing of one of my effects was off, i can literally switch to Fmod and then back to Unity and immediately see the result of my alterations. It also seems apparent that using middleware such as this (or i've heard Wwise is also equally intuitive) grants the developer, and myself included, a great deal more flexibility and opportunity to edit sounds without going all the way back to a DAW, and bouncing down again. Needless to say, my workflow is so much faster because of it.
I've also loved the randomised feature of Fmod, whereby any sound can be made to sound slightly different each time it is heard. Taking a footstep recording i made for example, I was able to add further authenticity of uneven footsteps by randomising the pitch and volume of each playback.

I used this technique when creating footsteps for the first major boss in the game called "The Guardian". A big, over-encumbered husk of a monster. I also had fun rummaging through the garage for old tools and metal components for the “Guardian” (the first boss) footsteps. See below!

I also created a sword attack for our player, trying to sound different from the generic “woosh” I see in so many video games. I used a very “sharp” and abrasive sound to differentiate him from any enemies.

On another note, I recently upgraded my microphone to a Rode NTG2 shotgun, which has been phenomenal. I haven’t had to worry about noise interfering with the clarity of my objects, whereas before with the sm58 I had to be clever with my EQ and noise reduction plugins.
Important to note again that this still a “cheap” mic in comparison to most other products on the market, and all in all my entire setup is still very simple and affordable which I’m quite proud of. I’ve seen many musicians spend heaps of money on gear they don’t necessarily need. I much prefer being resourceful with less equipment, than to have more than I can understand or remember how to use.
It’s forced me to understand every aspect and capability of my tools, which I believe is a principal that can be applied to any discipline.

I have more fun little sound effect videos on my Instagram for those interested, where I post regular updates. Thanks for reading! (if you’ve made it this far)

www.sergioronchetti.com
INSTAGRAM
fallenflagstudio.com

• BASICS IN SOUND DESIGNING FOR VIDEO GAMES

Recently I joined the talented team at Fallen Flag Studio as the composer for their latest release "Eldest Souls" which consequently lead me into a field I have always dreamt of trying - sound design!
Having no prior experience, I began watching a few online tutorials (if you want to learn from anyone make it Akash Thakkar from "Hyper Light Drifter"... what a guy!) and basically just testing stuff out i found around the house. Luckily my dad has a garage FULL of random crap to use.
Before i continue, it's important to note that i DO NOT have fancy equipment, meaning anyone can try this. (my equipment is an sm58, focusrite scarlett interface and Logic Pro X plugins... that's it!)
I started basic with some footsteps, which weren't all too difficult. Then I moved on to projectiles and a spear attack one of the bosses has. Below are a couple super short videos on my resulting attempts.

Amazing how great a banjo sounds for that typical "woosh" sound! And if you're wondering, the paper was added to give some texture to the jab.
I could be finding a lot of these sounds in libraries online (like the built-in ones that come with Fmod and Unity) but I've chosen not to, in order to produce authenticity and hopefully a more unique gameplay experience for players when the final product is put together.

P.S. if you'd like to try the game and hear my hard work we'll be at EGX and several other conventions later this year, soon to be announced! Thanks for reading!
www.sergioronchetti.com
fallenflagstudio.com

To those interested, there's an Alpha trailer of the game in question below.

• Hello there!
I’m the creator and producer of an upcoming visual novel / video game.
My team and I are looking for artists (character and background), writers (experienced in writing relatable characters and witty dialogue), and programmers (familiar with unity and creating mini games).
Our team is a group of close friends looking to break the mold of the traditional visual novel and create something new and positive. This game will be highly promoted and be a great portfolio piece. Rates are negotiable!

• Hi there,
i recently start learning unity and im working in my first game ,

I was wondering if unity had functions to support the motion control effect (tilting screen to move character) you see
in doodle jump (which is 2d game) ? If it exists, what are they called? and how it works ?

Thanks
×