‘Red of Paw’ – Tower Defence game – Part 2

Image

Previously I had my Red Panda soldier up and running, with animation, rigged and setup in Mecanim. From here I wanted to achieve two things:

  • Ability to click and select from multiple player ‘units’ and be able to point and click where the player wanted them to go.
  • A test ‘level’ where cubes stood in for ‘enemies’ looking to reach the Red Panda ‘base’.

‘Point and Click’ select and movement The first task was to simply allow the player to click on the screen and to move the panda around, and then for when the ‘enemy’ gets in range for the panda to shoot at it. This was a more complicated task than I had at first anticipated. Not only did the panda have to walk to a clicked location, but also had to be able to navigate through a map to get there.  For the navigation solution I chose RAIN AI (http://rivaltheory.com/tag/rain-ai/) – a free AI solution from Ribal{Theory}. It allows for a navigation mesh or for waypoints, although in it’s Beta state is limited while they implement certain features. However for a Tower Defence game it is ideal. The character is given an ‘AI’ core, which interacts with Mecanim very nicely, and then uses a ‘Behaviour Tree’ to direct it as to what to do. You can assign ‘senses’ such as sight, or sound, and assign these properties to other entities. You then ‘detect’ these entities and create a behaviour based on that. There are two loops present for the Panda:

  • Movement. There was no direct way to Raycast a location for the Panda to go and navigate at the same time – and throwing in other controllable units would add a complexity to this. The solution was to create an object for the panda to follow around, navigating the scene until it reaches it. The object is directly controlled by going to the point where you click. When the Panda gets there the object teleports away and so it no longer moves. If the object is not in range (because it has reached it’s destination) then the Panda will skip over to the second loop.
  • Target and shoot at the enemy. When the panda is standing still it’s sensor is keeping an eye out for an enemy, in this case our cube. If it finds one then it rotates towards it and then puts Mecanim into the ‘shoot’ state. A custom action is called in the Behaviour Tree to start some particle effects and send the ‘damage’ message to the cube. Once the target is out of range, or destroyed, it reverts back to an idle state.

The RAIN AI Behaviour Tree for this is here with some notes explaining each step:

Image

At this stage we now have our Panda working. I have to say however that getting to this point took a LOT of trial and error, and I have little experience with coding. What I have achieved here may well be possible in far simpler, more efficient steps, however it does work, and that’s good enough for me for now!

The ‘Enemy’ – cubes as placeholders. In comparison to the Pandas the ‘Enemy’ cube behaviour is very simple. They also use RAIN AI, but to far simpler effect. The behaviour tree for comparison is as follows:

Image

Simple! This shows how easy RAIN AI can create behaviour.  I then created a script to spawn a select number of cubes, along with another script to destroy the cube when it reaches the end. This behaviour could then be augmented to keep score, drop power ups or any number of options to expand gameplay.

Test Level and Navigation To test the cubes I made a simple maze, with the start and end points at opposite ends:

Image

To create a Navmesh, RAIN uses a Navmesh object, with a controller you re-size to match the area you want your Navigation to cover. After some calculation it then spits out the mesh, visible here in green:

Image

RAIN AIs will automatically use a Navmesh that is present. Additional functionality is available to chose the walking distance from the walls and further functionality is to be added in the future. Once the spawn script and everything else is in place we set everything running. Below you can see the ‘optimum’ path an object can take in green and the actual path it will take in Red. I may have this the wrong way around… in any case there is some good visual feedback for you to adapt settings of the Navmesh and AI.

Image

This basic layout I used to test the navigation and basic spawn behaviour of the Enemy cubes. Next I wanted to put them together with the Red Pandas in a more game-like setting.

Test level – something basic Below you can see a very simple mesh created in 3DStudio, with simple colours showing the Red Panda ‘base’ in blue, and two entry points for the faster Purple cubes and slightly slower Red cubes. These are arbitrary setups and much more work would want to go into creating a compelling game experience.

Image

The navmesh was created just as before. The coloured areas don’t actually ‘do’ anything – the ‘start’ points for each type of enemy (slightly different pre-fabs) and the blue is the ‘end’ point. If desired a condition could be set up to add/remove points as the enemy reaches the end.

Selecting the Red Pandas As you can see we now have two Red Pandas. We don’t want them to both move at the same time to our ‘move’ click points and so a selection method must be added. The basic premise is as follows:

  • Raycast.
  • If the raycast hits Red Panda 1 then we are in control of the ‘Point and Click’ cube for RP1.
  • If it hits anything BUT Red Panda 1 then we have no control over the Red Panda.
  • Repeat for Red Panda 2.
  • Left click is for selecting. If one of the pandas is selected then Right Click moves it’s ‘Point and Click’ cube.

Of course we also want to signify to the player which Panda is selected, and so a simple circle of green colour is switched on and off in exactly the same way as above.

Image

We now have our controllable ‘units’ and a relentless (well, at least until it’s spawn count is down to 0) heading towards it.  The player can move the Pandas, and if the Panda remains in one spot it will automatically attack any enemy it sees, just like a Turret in a Tower Defence game. There’s one final step I wanted to take – destroying the Enemy when it gets shot.

Enemy Destruction – cube goes boom When a Red Panda shoots it fires a Raycast from the gun. If it hits an enemy inside it’s preset range then it will set off a ‘tracer’ particle system for gunfire. It will then Instantiate ‘impact’ sparks on point where it hits the enemy. Finally it will then ‘send a message’ to the Enemy to interact with a ‘Health’ script and remove a set number of health points. The amount of damage and health points can be changed of course.  When the cube runs out of health it runs a ‘destroy’ script – which briefly teleports it away, putting in an explosion, before destroying the object itself.  This looks as follows:

Image

The explosion is a simple particle prefab. The smaller blocks that ‘explode’ outwards (as if the larger cube has shattered) are a simple prefab of a number of cubes arranged to match the size/shape of the original object. To achieve the ‘explosion’ physics outwards, a spherical collider is set to grow very quickly as the object appears, sending the cubes flying in all directions.

A timer is set on this ‘debris’ to ensure it will only stick around for a set amount of time before being destroyed.

A wider view of the action is seen here:

Image

Here we can see the two lines of Enemy cubes finding their way to the end point (off camera to the right), with the Purple ones going quicker. The Red ones are slightly bigger and have more cube ‘debris’ explode out as they ‘die’. We can also see the further away Panda is the one that is selected, and finally we can see debris falling off the level into infinite nothingness below.

The next steps for this game include:

  • Create a more interesting environment for the game to play out in.
  • Better Camera system.
  • Additional player unit ‘classes’
  • Score system (for keeping your ‘base’ defended and for destroying the enemy).
  • ‘Money’ drop system for in-game purchases (no real money involved!)
  • Static towers (bought for money picked up from dead enemies)
  • Upgrades (for units and towers)
  • GUI to keep track of the above.
  • Enemies slightly more interesting than cubes. Maybe. Unless cubes terrify you. Cubes can be pretty scary. If they are heavy and fall on you.
About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s