Creators – Adding Sound

This week we spent some time adding sound to our game.

Recording

We needed a rolling noise and a falling pin noise. I worked on these sound at home.

img_3119

To get a rolling sound, I recorded several different objects rolling, but settled on a roll of duct tape rolling across a wooden table top. It had the pace and tone I was looking for.  For the falling/impact sound, I recorded a wooden muddler (visible above the duct tape above) falling over on the same wooden table.

The original sound files are available here.

Updating Our Sound Files For Game Use

We first loaded the rolling sound into Audacity.

Screen Shot 2016-10-27 at 20.33.20.png

The sound clearly had quiet portions at either end and the sound ramped up and down either side the relatively steady portion in the middle. We selected a reasonably steady central portion of the sound and removed the rest.

This sound was intended to loop seamlessly. Normally the start and end of a sound don’t line up well and there’s a clear point at which the loop starts again.

A useful property of this rolling sound is that sounds much the same backwards as forwards. We were able to take advantage of that to produce a smoothly looping sound. We copied the sound, pasted it to a new track, reversed the new track and mixed them together. This meant that either end of the new sound was identical and the looping was seamless.

With the impact sound, the process was simpler. The recording had a lot of silence at either end and an impact noise with two peaks:

screen-shot-2016-10-27-at-20-49-35

This had resulted from the muddler bouncing after the initial impact and coming down a moment earlier to hit the table a second time.

Since we only wanted a single impact noise, we just cut the sound to exclude the first impact.

In both cases these audio files were exported from Audacity as WAV files for use in Unity and we placed them in an Audio subfolder of our project. The edited files are here.

 

 

Adding The Sound Files To Our Game

We first worked on adding the rolling sound to our sphere. We wanted two things:

  • The sound should be louder as the sphere rolls faster
  • The sound pitch should rise as the sphere rolls faster

To provide a place for the sound to play from, we added an AudioSource component to our sphere and assigned the Rolling sound to it’s AudioClip property. We also made sure that the volume was set to zero and the “Play on Awake” and “Loop” options were both selected.

In our existing PushSphere script, we added two new public properties:

   public float VelocityVolumeFactor = 0.1f;
   public float VelocityPitchFactor = 0.1f;

these are used to relate the sphere’s speed to the sound’s volume and pitch.

We also added a private AudioSource property and got it in the Start() method:

   private AudioSource audioSource;
 
   // Use this for initialization
   void Start () 
   {
     :   :   :
     audioSource = GetComponent<AudioSource> ();
   }

Finally, in the Update() method, where it will be called every frame, we set the AudioSource’s pitch and velocity based on the sphere’s speed (a.k.a. the magnitude of the velocity):

   void Update () 
   {
     :    :    :
     audioSource.volume = rb.velocity.magnitude * 
                          VelocityVolumeFactor;
     audioSource.pitch = rb.velocity.magnitude * 
                         VelocityPitchFactor;
   }

We then tested the game. We found that the rolling sound is pretty quiet and isn’t always that apparent, but I was able to clearly hear it at home.

For things that make a sound when the sphere impacts them, we created a new script called SoundOnImpact. The code in this is very similar to the new code we added to PushSphere and it’s here in its entirety:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class SoundOnImpact : MonoBehaviour 
 {
   public float VelocityVolumeFactor = 0.1f;
   public float VelocityPitchFactor = 0.1f;
 
   private AudioSource audioSource;
 
   // Use this for initialization
   void Start () 
   {
     audioSource = GetComponent<AudioSource> ();
   }
   
   void OnCollisionEnter(Collision collision)
   {
     audioSource.volume = collision.relativeVelocity.magnitude * 
                          VelocityVolumeFactor;
     audioSource.pitch = collision.relativeVelocity.magnitude * 
                         VelocityPitchFactor;
 
     audioSource.Play ();
   }
 }

Note that the code assumes that the game object this is attached to has both an AudioSource component and a collider of some kind. When a collision is detected, the sound is played. The volume and pitch of this sound are proportional to the impact velocity, in a manner similar to before.

We attached this script, and an AudioSource component, to both the walls and the pin prefab.

Project  Files

The full project files can be found here.

 

 

 

Bodgers – Finishing Our Automonous Robots

bodgers

This week we worked on the function calls for our robot, however as most of the robots were low on battery power we didn’t get a chance to test them properly. I will have new batteries when we return on November 5th and we’ll spend a while running through the obstacle course. You can find code with the finished function calls on Dropbox.

We also talked about The Future Maker Awards which is a competition being run by CoderDojo and Liberty Global. I mentioned that you can use video as part of your entry this is an example of a video from Google Science Fair.

Here are my notes from last week obstacle-course.

See you all after the break. Happy Halloween.

 

Week 6 – Explorers– Finishing off our Ghostcatcher game

Hi everyone,

We finished off our Ghostbusters game this week, there are no new notes, last weeks notes contain all the code you will need. I hope you will continue with your game that you started on Saturday, I would love to see them all when we come back after our Halloween break. Here is a link to the Cheat Sheets I gave out if anyone wants them. cheatsheet.pdf

 

See you all on the 5th of November

Martha

02

 

 

Hackers – Arduino Basics

arduino

On 15 Oct, we spent the session figuring out the basics of programming and electronics with an Arduino micro-controller. We had a couple of Arduino Unos (pictured above) and a Genuino 101.

We downloaded the Arduino IDE (integrated development environment) software here: https://www.arduino.cc/en/Main/Software

We then built a simple circuit to turn on an LED, using a breadboard:

Circuit.jpg

After connecting the 5V side of the circuit to Digital pin 2 on the Arduino, we started experimenting with writing code to control it, using the C-based language of the Arduino IDE:

arduinocode

After that, group members started experimenting with more complex programs and hardware, such as pressure and temperature sensors, which they had working by the end of the 2-hour session. I was very impressed with the speed with which they made progress!

 

Creators – Getting User Input

This week we worked on getting input from the user and making the game react to it.

InputManager

Unity has a build in system for handling user input. The InputManager can be found under the Edit|Project Settings|Input menu:

Screen Shot 2016-10-17 at 21.21.01.png

The Unity input system is based on the concept of “axes”. The game request if there is input on a specific axis, giving the axis name. What that means in terms of button presses, mouse or controller input depends on the settings in the InputManager. Axes have a value that ranges from -1.0f (full negative, if negative values are allowed) through 0.0f (no input) to 1.0f (full positive).

inputaxes

Pre-defined Input Axes

While the game programmer can define as many input axes themselves as they like, Unity come with many common ones built-in. The first two shown above, called “Horizontal” and “Vertical” are commonly used for movement. The third “Fire1” is usually used for shooting.

We redefined “Fire1” from it’s default key (Left Ctrl) to bind it to the space key instead.

screen-shot-2016-10-17-at-21-36-26

Updating the Code to Detect “Fire1”

We originally had a force applied to our ball in the “PushSphere” script as soon as the game started. We modified the code so that in the Start() method all we did was to retrieve and store the Rigidbody component. In the Update() method (called every time the game draws) we ask if there’s been any input on “Fire1” and only if there has, do we apply a force to the ball:

 using UnityEngine;
 using System.Collections;
 
 public class PushSphere : MonoBehaviour 
 {
   public float PushStrength = 1.0f;
   private Rigidbody rb;
 
   // Use this for initialization
   void Start ()
   {
     rb = GetComponent<Rigidbody> ();
   }
   
   // Update is called once per frame
   void Update () 
   {
     float fire = Input.GetAxis ("Fire1"); 
 
     if (fire > 0.0f)
     {
       rb.AddForce (Vector3.back * PushStrength);
     }
   }
 }

Before proceeding, we ran our game and tested to ensure it  was working correctly.

Moving Our Aimer

The aimer was rotated using input from the horizontal axis. We added a script called AimerController to our Aimer game object. A public property called RotateSpeed gives us a way to tune the speed our our aimer. We don’t need anything in the Start() method, and it’s been removed here for clarity. The Update() method looks for input on the “Horizontal” axis (which maps to the left and right keys) and applies a rotation to the Aimer’s transform using the following logic:

amount to rotate = speed of rotation * user input * time

User input here will either be -1.0f, 0.0f or 0.0f. This equates to rotate in a negative direction (anti-clockwise), don’t rotate or rotate in a positive direction (clockwise).

 using UnityEngine;
 using System.Collections;
 
 public class AimerController : MonoBehaviour {
 
   public float RotateSpeed = 1.0f;
    
   // Update is called once per frame 
   void Update () {
     float horiz = Input.GetAxis("Horizontal");
 
     transform.RotateAround (transform.position, Vector3.up, 
                             RotateSpeed * horiz * Time.deltaTime);
   }
 }

Note that we’re rotating about the vertical axis and the rotation is centred around the aimer’s position.

Linking the Aimer and the Sphere’s Direction of Fire

To link the aimer’s with the sphere’s direction of fire, we needed to provide the sphere with a new public property which could store a reference to the Aimer’s game object:

public GameObject Aimer;

We then simply had to update the AddForce call, in the Update() method, to take the aimer’s direction into account:

 rb.AddForce (Aimer.transform.forward * -1.0f * PushStrength);

Note that we had to also multiply by -1.0f as the ball was initially going opposite to the direction we wanted.

Prefabs

Prefabs are objects, or collections of objects that we want to re-use multiple times. To create a prefab, all we need to do is to drag an object from the Hierarchy to a project folder. We made a prefab of our pin by doing this. Note that prefabs show up blue in the hierarchy to mark them out. When the prefab is changed, all copies of it in the hierarchy are updated as well.

Automatic Creation of Game Objects at Run-Time

Once we created our pin prefab, we worked on adding pins automatically into the game a run-time.

We added a new empty game object and named it PinManager, we then added a component to it of the same name. This script was given a public property, of type GameObject, to store a reference to the prefab. In the Start() method, we use two loops to place nine pins automatically into the scene, using the Instantiate() method:

 using UnityEngine;
 using System.Collections;
 
 public class PinManager : MonoBehaviour 
 {
   public GameObject PinPrefab;
 
   // Use this for initialization
   void Start () 
   {
     for (int x = -1; x <= 1; x++)
     {
       for (int z = -2; z <= 0; z++)
       {
         Vector3 pos = new Vector3(x * 2, 0.5f, z * 2);
 
         Instantiate (PinPrefab, pos, Quaternion.identity);
       }
     }
   }
 }

Note that there are several versions of the Instantiate() method. The version we’re using allows us to specify the position and rotation of the newly created object. A Quaternion is variable type that can store a rotation. Quaternion.identity simply means “no rotation”.

Testing

Project File

The latest project files can be downloaded from here.

Bodgers – Finishing Our Robot Functions

iconHi Everyone

This week we created our own folders on our Raspberry Pi3s, this will allow us to each find our own code and  keep working on it.

We finished off the code for our own robot functions which we will use to navigate an obstacle course next week. The finished code is available on Dropbox here. These are my slides from Saturday finished-functions.

After we are finished with the obstacle course we will look at controlling our robot from our keyboard. I will use Pygame to do this and as Pygame is used for Graphical applications such as games and GUIs we will have to use VNC to connect to our Pi3s. Pygame will also allow us to use a webcam to capture video so if you have an old USB webcam please bring it in.

CoderDojo and Liberty Global are due to launch the Future Maker Awards this week FAQ here and more information here scroll down to see how one of our Bodgers uses his super power. I’ll have more information next Saturday after the launch.

See you then.

 

Week 5 – Explorers– Improved Ghostcatcher game

Hi again everyone,

This week we improved on our Ghostcatcher game from last week.

We introduced a new concepts which we haven’t used before, animation and next week we will start another new concept which is broadcasting.

Using a simple technique of changing between costumes and having a waiting time we can animate our Sprites. In our case, we animated the Ghosts so they were blinking or sticking out their tongue. You could of course use this technique to have a sprite look like they are walking etc.

animation

Hope you all enjoyed the session and we will be using animation and broadcasting over the next couple of months.

Here are this weeks notes in PDF  cda-s6-week_05-betterghostcatcher.pdf

Hope to see you all again next week

Martha

Image editing with the Advancers

wilber-bigThis week at Advancers, we took some time out from programming to review image editing, and play around using the highest quality free image editor in the world, the GNU Image Manipulation Program, also known as “The Gimp“.

Mastering Gimp, or a tool like it, is essential to be able to produce the images you will need for high quality games.  It’s also very useful for touching up photographs, chopping people’s heads off and sticking them on others, etc!

After taking some time to get it installed on most people’s laptops, we reviewed the most important parts of this incredibly powerful program:

  1. Installation of Gimp
  2. The basics of how to open and create a new image file
  3. The Gimp toolbox, how to change colors and use the brushes to paint.
  4. Layers and how they can make your life so much easier when it comes to image editing
  5. Selections, what they are used for and how to use them

Installing Gimp

On windows or Mac, GIMP can be installed by browsing to https://www.gimp.org/ and selecting “Download”.  On linux it can be installed from the app store/package manager on your distribution.  Note: some people last week had Chromebooks and I mistakenly told them that gimp wasn’t available on those devices – I since checked and it is available, though as it’s a web-app it is not as good as a locally installed version – check out this video for instructions on installation: https://www.youtube.com/watch?v=NtdcukXILJg .  It is also not quite free too since you need to pay to save your files.  Click read more for more details!

Continue reading

Creators – Starting Our First Game

This week we started our first game in earnest. Let’s talk about the design and the steps we took to begin implementing it.

PinBowling.jpg

Game Design

The game involves a simple play area with pins. The players tries to knock down the pins by controlling the direction and force at which the ball is projected. The number of pins knocked constitutes the score. The game shares element of classic games such as pinball, bowling and skittles, to name a few.

Setting up the play area

To establish the play area, we used a plane (for the ground) and four cubes stretched to size to provide the walls:

gameboard

Because we don’t want gaps in our walls, we’ve been careful to be precise with the positions of everything.

Applying a Force to our Sphere

We added a sphere to our scene and made sure that it had a RigidBody body component. The RigidBody component makes it so that the physic engine takes care of the movement of the sphere.

To test adding a force to the sphere we added a temporary script to the sphere and had it add a force to the RigidBody as soon as the game starts.

Immediately it was clear that the force was too weak, so we added a property to the script which allowed us to scale the force. In testing 1000 proved to be a good value. Here is the script:

 using UnityEngine;
 using System.Collections;
 
 public class PushSphere : MonoBehaviour 
 {
   public float PushStrength = 1.0f;
 
   // Use this for initialization
   void Start ()
   {
     Rigidbody rb;
     rb = GetComponent<Rigidbody> ();
     rb.AddForce (Vector3.back * PushStrength);
   }
   
   // Update is called once per frame
   void Update () 
   {
   }
 }

 

We also experimented with creating a physic material and assigning it to the sphere. Different levels of friction and bounciness give different behaviours.

Testing With a Single Pin

To test the sphere colliding with a pin we added a single cylinder to the scene. Like the sphere, we made sure it had a RigidBody component attached to it. When the game started the ball shot forward and knocked the pin, as we’d hoped.

Aimer

To aim our sphere and control the strength of our shot, we created an aimer. The aimer is composed of an empty object containing two cylinders, one pointing forwards and one pointing crossways.

We didn’t want the aimer cylinders interacting with anything so we made sure to remove the colliders from the cylinders.

We also wanted the aimer to be partially transparent so we created a new material, setting the Rendering Mode to “Transparent”, choosing a colour from the Albedo colour picker and setting the Alpha value to a value less than 255.

Screen Shot 2016-10-10 at 22.39.21.png

Project

Updated project files for this week’s project can be found here. This requires Unity Version 5.4.1f1 or later.