Everything's coming up explodey! (FPS Survival)

Rocket Launcher + SMG = Rocket Launcher + SMG. Unless the SMG fires explosive bullets.
Only time will tell...

Week five, continuing with the FPS Survival brief. We now have a second gun! As hinted above, it's a Sub machine gun. We were told to implement a way of switching weapons (after playing around with it) between the RPG and SMG. The mechanic of switching weapons is easy to think about, you'd think the player simply puts their weapon on their back or in a holster, but that's mostly true for games that have the budget to show what the player is currently holding.

For us, it's just a matter of literally changing the object the player is holding and not worrying about what's already on their back, what they currently having out or where to display the holstered weapons. It does have me thinking on a method of doing these though...



I ended up making use of Unity's enabled option (or "Active") to switch between them. Both guns are in the same location as each other but one is simply disabled at any given moment. Simple code is simple...

//Switch the weapon if the player wants to
void SwitchWeapon() {  
    if(Input.GetKeyDown("1")) {
        weapons[0].SetActive(true);
        weapons[1].SetActive(false);
    } else if(Input.GetKeyDown("2")) {
        weapons[1].SetActive(true);
        weapons[0].SetActive(false);
    }
}

The weapons array just consists of the SMG and RPG GameObjects, but can be expanded to any size providing there is a proper method of cycling between those weapons. Maybe an iterator variable can keep track of what you currently have out...

Next was a scoring system. This became pretty trivial since all you need to do is setup a variable and add to it once an Enemy dies, then a UI Text element to display the score to the player.
But what was probably the most interesting task to me would have been item pickup. This case it was a Health pickup but still acted like an item on the ground. We created a script so when the OnTriggerEnter function gets called on the pickup, it'll check for the Player's tag, if it finds it on the GameObject. It will call the AddHealth function on the player which is below and destroy the pickup.

public void AddHealth(float hp) {  
    health += hp;
    if(health > maxHealth) health = maxHealth;
}

I found it surprising that I never used had a drop/pickup system in any of my games for assessment in this and other subjects. So hopefully I'll end up using it if I can remember to implement it. That and explosive bullets for the SMG, because then it's a real game.

Tom Lynn

Read more posts by this author.

Australia http://rubbix.net