Lesson 3.1 - Sound and Effects
Open prototype & change background
- Open Unity Hub and create an empty “Prototype 3” project in your course directory on the correct Unity version. If you forget how to do this, refer to the instructions in Lesson 1.1 - Step 1
- Click to download the Prototype 3 Starter Files, extract the compressed folder, and then import the .unitypackage into your project. If you forget how to do this, refer to the instructions in Lesson 1.1 - Step 2
- Open the Prototype 3 scene and delete the Sample Scene without saving
- Select the Background object in the hierarchy, then in the Sprite Renderer component > Sprite, select the _City, _Nature, or _Town image
Choose and set up a player character
- From Course Library > Characters, Drag a character into the hierarchy, rename it “Player”, then rotate it on the Y axis to face to the right
- Add a Rigid Body component
- Add a box collider, then edit the collider bounds
- Create a new “Scripts” folder in Assets, create a “PlayerController” script inside, and attach it to the player
Make player jump at start
- In PlayerController.cs, declare a new private Rigidbody playerRb; variable
- In Start(), initialize playerRb = GetComponent<Rigidbody>();
- In Start(), use the AddForce method to make the player jump at the start of the game
Make player jump if spacebar pressed
- In Update() add an if-then statement checking if the spacebar is pressed
- Cut and paste the AddForce code from Start() into the if-statement
- Add the ForceMode.Impulse parameter to the AddForce call, then reduce force multiplier value
Tweak the jump force and gravity
- Replace the hardcoded value with a new public float jumpForce variable
- Add a new public float gravityModifier variable and in Start(), add Physics.gravity *= gravityModifier;
- In the inspector, tweak the gravityModifier, jumpForce, and Rigibody mass values to make it fun
Prevent player from double-jumping
- Add a new public bool isOnGround variable and set it equal to true
- In the if-statement making the player jump, set isOnGround = false, then test
- Add a condition && isOnGround to the if-statement
- Add a new void OnCollisionEnter method, set isOnGround = true in that method, then test
Make an obstacle and move it left
- From Course Library > Obstacles, add an obstacle, rename it “Obstacle”, and position it where it should spawn
- Apply a Rigid Body and Box Collider component, then edit the collider bounds to fit the obstacle
- Create a new “Prefabs” folder and drag it in to create a new Original Prefab
- Create a new “MoveLeft” script, apply it to the obstacle, and open it
- In MoveLeft.cs, write the code to Translate it to the left according to the speed variable
- Apply the MoveLeft script to the Background
Create a spawn manager
- Create a new “Spawn Manager” empty object, then apply a new SpawnManager.cs script to it
- In SpawnManager.cs, declare a new public GameObject obstaclePrefab;, then assign your prefab to the new variable in the inspector
- Declare a new private Vector3 spawnPos at your spawn location
- In Start(), Instantiate a new obstacle prefab, then delete your prefab from the scene and test
Spawn obstacles at intervals
- Create a new void SpawnObstacle method, then move the Instantiate call inside it
- Create new float variables for startDelay and repeatRate
- Have your obstacles spawn on intervals using the InvokeRepeating() method
- In the Player Rigid Body component, expand Constraints, then Freeze all but the Y position