Alright, like I’ve promised…enemies are now in the game! But here’s the thing…its a first attempt at this sort of thing, so it’s starting out simple.
Getting started, as you can see in the featured image above, the enemy is represented by a standard capsule game item. This is just to get the mechanics down prior to implementing a more complex, moving and textured/modeled enemy. Upon creating that enemy, two scripts are needed:
- Enemy Health
- Damage per Attack
So this script does a few things: First, it sets up an attack damage value. This is set up by the hitpoints variable, which is currently set for 10 damage per hit. You can name this whatever you want, just make sure you remember what its called to reference later. Secondly, it gives a range of attack. If you are standing outside of that range, the damage will not be applied to the enemy or target. This is of course represented by the variable range. Down later in the code a variable DeductPoints is reference which will appear later in the next script.
So, speaking of the next script, this second one will go into the capsule that was created to allow for health points, deduction of those points and ultimatley the ability to destory the enemy once it’s killed or health reaches zero. Here is what that code should look similar to:
With this, we’ve given the enemy 50 health, which if you remember earlier, we declared our weapon to do 10 damage. This means when the simulation is running, we should be able to hit the object 5 times and it will be ‘destroyed’. The other thing I want to point out is the DeductPoints which I mentioned in the last code. Here, its saying to deduct our attack damage (which is named hitpoints) and take it away from the enemeyhealth (set to 50) each time we are in range and attack with our weapon.
Now we have a stationary target which can be whacked to your hearts desire…until it dies of course. The values presented in this can be altered to your own specifications, but are valid for test purposes.