Supporting our Community:
We’re making Learn Premium available to all users in perpetuity.
Start Learning.

Week 5 | Session 4 - For-Loops for Waves - April 23, 2020

Tutorial
Beginner
1 Hour
Overview
Tutorial Materials
Details
Summary
We have all the makings of a great game; A player that rolls around and rotates the camera, a powerup that grants super strength, and an enemy that chases the player until the bitter end. In this lesson we will wrap things up by putting these pieces together!
First we will enhance the enemy spawn manager, allowing it to spawn multiple enemies and increase their number every time a wave is defeated. Lastly we will spawn the powerup with every wave, giving the player a chance to fight back against the ever-increasing horde of enemies.
Select your Unity version
Last updated: April 27, 2020
2018.4
Language
English
Tutorial
Week 5 | Session 4 - For-Loops for Waves - April 23, 2020
2
Prototype4 Upgrades
Lots of upgrades, including: JumpBlast, Speed Boost, and Projectiles (which destroy enemies)! The player has multiple lives, with automatic respawn after falling off the edge. There are three enemy types, differing in health, size and speed. Not featured: number of enemies killed and Game Over message which appears in Console window upon final death. The most interesting challenge was programming JumpBlast. After trying several different approaches (including animation, which didn't play nice with Physics), I found that calling a coroutine was the best solution: IEnumerator JumpBlastForceRoutine() { // Allow the player time to jump into the air playerRigid.AddForce(Vector3.up * powerupForce, ForceMode.Impulse); yield return new WaitForSeconds(0.4f); // Make the player slam quickly back to the ground playerRigid.AddForce(Vector3.down * powerupForce * 3, ForceMode.Impulse); } In OnCollisionEnter, I added the following code to play the blast particles and push away any enemies that might be too close to the player: int enemyLayerMask = 1 << LayerMask.NameToLayer("Enemy"); if(collision.gameObject.CompareTag("Ground")) { isOnGround = true; if (hasPowerup) { blastParticles.Play(); // Push away all nearby enemies with JumpBlast Collider[] nearbyEnemies = Physics.OverlapSphere(transform.position, blastRadius, enemyLayerMask); foreach (Collider enemyCollider in nearbyEnemies) { Debug.Log(enemyCollider.gameObject + " is within blast radius"); Rigidbody enemyRigid = enemyCollider.gameObject.GetComponent<Rigidbody>(); Vector3 awayFromPlayer = enemyCollider.gameObject.transform.position - transform.position; enemyRigid.AddForce(awayFromPlayer * powerupForce, ForceMode.Impulse); } } } This was a pretty fun challenge to complete. Thanks for the great instructions! ~AH
23
0
Week 5 | Session 4 - For-Loops for Waves - April 23, 2020