5 Weapon and other advanced game functions
- Find source project Tutorial_4_Upgrade in Shooter
Weapon compositions
Gun + bullet
- Gun: shoot frequency, ammo, fire direction
- Bullet: damage, speed, damage type
Player to control it
- Player script to handle weapon change, fire, and pick up items
Weapon
lastShootTime
and freezeTime
to implement weapon freeze time。
Calculate fire direction
Calculate fire direction based on mouse position
Bullet
In Update
to let bullet move in a particular speed. Set bullet game object collider as trigger
type. When collider with others, then check if cause damage.
Grab a gun
Characters can equip a particular weapon.
Cause damage to characters
The bullet from the player will cause damage to enemies, and vice verse.
Different type of bullet will cause different type of damage.
- Common bullet will cause instant damage
- Explosion bullet will damage all characters inside the efficient region
- Consistent bullet will cause damage for a while
- Puncture bullet wouldn’t stop when hit a character
Change weapon
Use prefabs
Weapon system logic
Instantiate all possible weapons for every character.
But only one of them is currently useable.
- The others are disable
- When the player pick up weapon, corresponding weapon in its weapon list will become usable
When fire, current weapon will try to instantiate a bullet, then bullet script will take over the following process.
- Enemy will fire at the player
- Player will fire at the direction where the mouse point to
Set enemy behavior
Set the enemy tag as Enemy
. Use enemy script to receive damage when shot. When the health of enemy below to 0, die. There is some probability to drop an item.
Next turoial please click Make it complete.
More details, please visit the project here