Class hours: 9:40 – 2:05
Mr. Bohmann
wbohmann@ewsd.org
Week Twenty Eight (28)
Today’s Notes
- Today is an EHS A Day
- The end of Quarter 3 is March 31st, that’s Friday
- Overdue / past due work deadline is 2:05 Friday – Check PowerSchool
9:45 Unity – Pinball
Yesterday we used the Unity physics engine to create realistic collisions between objects.
Collisions are an important part of gameplay. If the player shoots a target, your game might update a score, or if the player hits an obstacle, he or she might lose some health, or if there is a goal or a basket, the player should be rewarded! This game logic requires more than just the physics engine – you also need to write some scripts.
Besides collisions, there are other event driven programming:
- Player clicks on a button
- Timer countdown reaches zero
- Player health reaches zero
- Player wins the game
- …and of course, a collision between objects!
OnCollisionEnter2D
As soon as you add a Collider 2D component to your game object, Unity can run a specific function every time a collision occurs with that object. OnCollisionEnter2D() function can be added to your script.
When a function is called, it is possible to give that function some useful data. The function can then use that data to perform a task. The data is listed inside the function parentheses right after the function name.
For our example, (Collision2D otherObject) is the parameter. The first part (“Collision2D”) shows the type of data and the second part (“otherObject”) is the local name for that piece of data that we can use inside our function. Of course we could use the name “pizza” as our local name, but that doesn’t make much sense.
Once you add the OnCollisionEnter2D() function to your script and attach that script to a game object with a collider, the function will be called each time a new collision is detected with another object that also has a collider.
void OnCollisionEnter2D(Collision2D otherObject)
{
// Code here runs when collision occurs
Debug.Log("Collision with: " + otherObject.gameObject.name);
}
OnTriggerEnter2D
So far, all of our collisions have resulted in some real-world reaction from the objects that hit each other. Beach balls bounce and bowling pins go flying. However, you may want to detect collisions in cases where there should be no real-world physics reaction. Like when a ball crosses the goal line in soccer or your car jumps or crosses over a powerup. In those case you want to trigger an event.
With “Is Trigger” checked, your collider will detect collisions but game objects will not be part of the game physics when a collision happens. You will get an event-driven function call that you can use to run game logic, but the two objects will pass through each other on the screen.
void OnTriggerEnter2D(Collider2D otherObject)
{
// Code here runs when a trigger collision occurs
Debug.Log("Trigger with: " + otherObject.gameObject.name);
}
Pinball is a classic stand-up arcade game and one that I played a lot growing up. A steel ball is shot onto a playing surface and bounces between bumpers and other gadgets, scoring points. The player tries to keep the ball in play with flippers or paddles at the bottom. Making a Pinball game is a great chance to show off the physics engine in Unity and some of the skills you worked on the last few days with movement.
Create a New 2D scene (called) PinBall
Project Details
For our game, a ball will drop from the top of the screen to start the game. As it falls, it will bounce off a series of bumpers on the screen. The player will move a paddle to the left and right at the bottom of the screen to prevent the ball from falling down the “hole” at the bottom of the screen. Once the ball falls down the hole, the game is over.
The boundaries of the game are a series of rectangles with box colliders added.
You will add the rigidbody, collider, and physics material components to the ball, bumpers, and paddle to make the game work correctly! You will also write the C# script to control the paddle movement and detect when the game is over.
Let’s create a simple prototype. Later we an add our own look to customize our game. Let’s just get the mechanics working. A minimum viable product is what we are after. For reference, I’ve created my own basic prototype above. You can copy or create your own.
You will need:
- A Movement Script for your player so that when you use the A or D key, your paddle moves around
- A Ball Script for detecting game over (We’ll code together)
- Add RigidBody 2D to your pinball
- Use physics materials (that you create) to manage your bumpers
- PlayTest your game
- Challenge: Can you respawn your ball if you lose? Check out the RollerBall game we started a few weeks back
10:35 Break
10:45 PSA Final Session Sprint
Let’s look at how to publish your final project. Once your project is on YouTube, you will upload the link to Google Classroom Assignment Dropbox requesting your YouTube link.
My recommendation is to take feedback from our Last Looks session at 11:30 and incorporate before you submit to the contest.
11:30 PSA Last Looks Showcase
12:15 Lunch
12:45 Literacy in Practice
1:10 Break
1:20 20% Production Time & Guided Support
- SkillsUSA Practice
- PSA Final Publishing to YouTube
- Lip Sync project planning – Not due until April 12th
- Alien Dance
- Bowling – showing physics
- PinBall game