Skip to content
Cawd Logo GAWD
  • Home
  • About
  • Assignments
  • Resources
  • Contact

Friday, May 2nd

Friday, May 2nd

Class Hours: 10:05 – 2:40
Mr. Bohmann | wbohmann@ewsd.org

10:05 Today’s Notes & Attendance

  • Happy Friday – today is the midpoint of Quarter 4 –
  • Last Day to hand in work for Quarter 4 and the year is…. Friday, June 6th – end of day (that’s 5 weeks away)
  • May 3rd -11-4:00: VT Jobs Fair @ UMall
  • May 21 & 22: Free clothing pop up in CTE Conference Room
  • May 26th: Memorial Day- No School

10:10 Fixing the Rotation Problem – Pinball

Sometimes you are in your game(s) and some feature is driving you crazy. Well, Scotty identified a problem yesterday. To really learn how to code, you have to spend some time digging around and learning from your mistakes and the mistakes of others.

Problem: When our ball launches, it rotates. If it was rotating before it respawns, then the up is no longer the up and when the user presses the launch button it doesn’t go up 🙁

Solution: If we can reset the “up” of the ball’s rotation on Z Axis when we press the Launch Key, then, all is good. Unity has a function for this: public void SetRotation(float angle); So if we can reset the rotation angle to 0 degrees, we should be good!?

public void ResetRotation()
{
    rbBall.SetRotation(0.0F); //make a reference to the rigibody and set the //rotation angle
}

10:15 Using Instantiate() to Create PreFabs

image of rocket landing

We’ve seen how we can write code to destroy gameObjects. You are doing this for your Breakout game.

void OnCollisionEnter2D(Collision2D otherObject)
{
Destroy (otherObject.gameObject);
}

The Destroy() logic can be placed anywhere in your scripts, so long as you have a reference to the target object. You might remove objects in the OnCollisionEnter2D() and OnTriggerEnter2D() functions, or from the Update() function or in some other function – whatever makes sense to your game.

If we put that bit of code on your breakout ball, it would destroy any game object it collides with.

The Destroy() function can take a second parameter that allows you to delay the effect. You can pass in a number of seconds such as 0.5 or 2.0 and the object will not actually be destroyed until that much time passes.

void OnCollisionEnter2D(Collision2D otherObject)
{
Destroy (otherObject.gameObject, 1.0F);
}

We can also use code to create gameObjects too. This is really handy with PreFabs, spawning enemies, handling bullets managing random power-ups – I could go on. Let’s start basic then add some heavy lifting:

  1. Open your Lunar Lander game.
  2. Add an asteroid sprite to the hierarchy
    1. Add circle collider 2D
    2. If you have a rotation script, pop that on as well
    3. Drag to your PreFabs folder – you now have a prefab
    4. Delete your PreFab from the hierarchy
  3. Open your LunarLander Script and
  4. Create a Public GameObject asteroidPreFab variable
  5. Populate that new variable slot with your PreFab from the PreFabs folder
  6. In the void Start method of your LunarLander add in the following line of code
// create a variable that is public
public GameObject asteroidPreFab;


   void Start () 
   {
      Instantiate(asteroidPreFab); // create a new copy of the prefab
   }

We have chosen to call Instantiate() in the Start() function of this script, because we want the asteroid to be created right away when the game starts. But, you can call Instantiate() from anywhere that makes sense for your game! For example, if you are firing bullets from a gun, you might call Instantiate() from inside the Update() function when you detect the trigger key.

Level Up – Round 1

What happens if we run this script – where do the Asteroids go?

public GameObject asteroidPreFab;


   void Start () 
   {
      Instantiate(asteroidPreFab); // create a new copy of the prefab
      Instantiate(asteroidPreFab); //copy 2
      Instantiate(asteroidPreFab); // copy 3
      Instantiate(asteroidPreFab); // copy 4
      Instantiate(asteroidPreFab); // copy 5
      Instantiate(asteroidPreFab); // copy 6
   }

You can actually call Instantiate() with one or more additional parameters to configure the cloned object. This version lets us add a new position and rotation to the new object.

Instantiate (asteroidPreFab, new Vector3(4.0F,2.0F,0), Quaternion.identity); 

The Vector3 parameter contains the X, Y, and Z coordinates of the new object in world units. You can get a good sense of the X and Y units by looking at the grid lines within the camera rectangle on the scene. Each grid line is 1 world unit, so if your camera is showing 5 boxes vertically and 8.5 horizontally in every direction from the center.

ProTip: You can toggle grid lines on and off (look for the Y icon near the grid icon at the top of Scene)

The third parameter is a Quaternion that represents the rotation angles in the X, Y, and Z directions. A Quaternion is actually a complex object that you don’t want to try and modify yourself. Fortunately, if you don’t want your new object to be rotated, you can pass in the value Quaternion.identity, and the object will be created with no rotation.


Level Up Round 2

A 2D (“orthographic”) camera like the one we are using has two handy properties that we can read to find the size of the screen. The orthographicSize property shows us half of the screen height, in world coordinates. The aspect property gives us the ratio of width and height. So, we can easily calculate the screen dimensions as shown below. Aspect is calculated as width divided by height.

 // calculate the size of the screen, in world units
float worldHeight = Camera.main.orthographicSize * 2.0F;
float worldWidth = worldHeight * Camera.main.aspect;

We can get a new random value by simply writing Random.value as part of an expression. Random.value is a Unity property that will return a random number between 0.0 and 1.0 (including both 0.0 and 1.0 in the possible range). Let’s use this random value and some math to calculate random positions on the visible scene in world coordinates. However, to use this expression, we need to add a library to the top of our script where we want to use it – or we’ll get an error message.

using Random = UnityEngine.Random;
// calculate random starting location
float randomX = (worldWidth * Random.value) - worldWidth / 2.0F;
float randomY = (worldHeight * Random.value) - worldHeight / 2.0F

Each line will get a random number and then multiply that by the width or height of the screen. So if a screen is 14 world units wide, for example, we will have a random number between 0 and 14. We then subtract out half the width or height because the origin (0, 0) is in the middle of the screen. Subtracting half of 14 from a random number between 0 and 14 would give us a random number between -7 and +7.

Confused Yet? Probably with the math. So just copy the code and its all good man.

void Start () 
{

   // clone 3 asteroids at random locations
   CreateAsteroid();
   CreateAsteroid();
   CreateAsteroid();
}

void CreateAsteroid()
{
// calculate the size of the screen, in world units
float worldHeight = Camera.main.orthographicSize * 2.0F;
float worldWidth = worldHeight * Camera.main.aspect;


// calculate random starting location
float randomX = (worldWidth * Random.value) - worldWidth / 2.0F;
float randomY = (worldHeight * Random.value) - worldHeight / 2.0F;

Instantiate(asteroidPreFab, new Vector3(randomX,randomY,0), Quaternion.identity);

}

10:50 Morning Break (10 minutes)

11:00 One Button Game Pitch

Dropbox in Google Classroom

Talking Points

  • Why should we play it?
  • Game Name
  • Quick Summary
  • Which/What button – what is the core mechanic
  • Win Conditions
  • Lose Conditions

11:45 Pinball Game Play time

Pinball Game Publishing

12:15 CTE Student Survey

Please complete the following survey. It will be used by CTE to identify areas of improvement for the coming years. This survey was put together by CTE Administration.

12:25 Lunch

12:55 Independent Reading

book covers

1:20 Afternoon Break (10 minutes)

1:30 Passion Project or Independent work time

Google 20% graphic

Past Due work / BreakOut Game

2:38 Dailies

2:40 Dismissal

GAWD Instructors:

Matt Cronin

Will Bohmann

Instragram Facebook Twitter

A little about GAWD:

Serving high school students interested in Gaming, Animation, and Web Development in North Western Vermont.

Students continue at:

University of Vermont Ringling School of Art and Design Northeastern University Rochester Institute of Technology Concordia University

Students find careers at:

Dealer.com Union Street Media Rovers North Prudential Investments DockYard
Navigate to top of page