So last week we made sure Bob is an actual character who has his bones, and he’s using those to wander around. This week we thought it would be great if Bob has someone else in the glorious test-scene and he doesn’t feel alone.
Based on my previous modeling and animation experience, let’s just say it’s easier if we reuse the same model, with a little bit of twist in Unity: give the twin a color. What color should it be? Maybe a better question is: who will be the twin? To make Bob interact with someone else, in a game there’s a general idea: let’s make his arch-enemy become alive!
Okay, so we’ll do the following:
- Create a new Prefab in Unity using Bob as a character, EnemyBob borns
- Apply a different material so EnemyBob has a reddish color
- Create some scripts for the behaviors (proximity alert, chase, attack)
- Add new animation to the character in Blender for attacking
- Set up EnemyBob
- Enjoy the trip in the test-scene
In Unity, a Prefab is a game object which is set up with Components by default: scripts, animators, colliders, you name it. The main reason to use them (and reuse them) is that you can quickly populate your map with the same entity, also you can apply different settings for each instance if you like.
To make your game object a prefab, simply drag it from the Hierarchy to the Project browser. Keep in mind, if you need to change the settings for the Prefab you can open the Prefab itself or you can override the prefab settings from an actual instance as well. 1st step done, no sweat, continue.
Scripting: my favorite part. Let’s figure out what we need and what are the differences in comparison with Player Bob:
- Obviously: EnemyBob won’t be controlled by the player, we can get rid of PlayerController which handles the userinput
- We’ll need an EnemyController class (MonoBehavior)
- He’ll still need to use the same walking and running, also the Idle animations that are baked into the model, so we can reuse the same Animator as well, great. The animator on the player is controlled by the Movement script based on the current velocity and whether the NavMesh Agent is moving or not; the same can be reused here as well.
- The main difference is that EnemyBob needs to react if the player arrives near to him; first, just be alarmed, if the player comes closer he’ll start to chase him and finally beat him for good when he reaches his twin bro.
After some messing in Blender to have the model exported properly and working – I’m writing this 24 hours after I messed up the whole Blender file – I’ve added the Attack animation to the Animator, added 2 lines of code, and an additional boolean switch to the Animator, I was ready to set up the scene for the first test run. The fun thing that I started to think about how I can advance the enemy concept from this state. I have a couple of ideas but one I like the best: using the fact that the enemy1 is chasing me, I can lure him to enemy2, and after that, enjoy the show how they slap each other forever.
As a finish I was messing with a couple of settings and this is the end result: