Creators – Week 13

This week we imported some assets for our gems, distributed random gems around the environment and created a digging action and effect.

Importing Gems

To avoid the tedious work of getting up all the gem definitions, I created an asset pack on our Teams site containing:

  1. Gem models
  2. Gem images
  3. Completed gem definitions
  4. Updated GemDefinitions.cs

To avoid clashes, we deleted our existing gem definitions and GemDefinitions.cs script before importing the asset pack.

Random Gem

We wanted to create a prefab which represented a random gem. First though, we need to get the gem definitions into memory. We create new class called GemManager.cs and attach it to a new object in the scene, also called ‘Gem Manager’. GemManager.cs has an array for storing all the gem definitions and a single function to return one at random:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GemManager : MonoBehaviour
{
    public GemDefinition[] GemDefinitions;

    public GemDefinition RandomDefinition()
    {
        int index = Random.Range(0, GemDefinitions.Length);
        return GemDefinitions[index];
    }
}

In the scene we set the size of the array to 15 and added all our gem definitions to it.

We then created our RandomGem.cs:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RandomGem : MonoBehaviour
{
    public GemDefinition GemDefinition;

    // Start is called before the first frame update
    void Start()
    {
        GemManager gm = FindObjectOfType<GemManager>();

        if (gm != null)
        {
            GemDefinition = gm.RandomDefinition();
        }
    }

    private void OnDrawGizmos()
    {
        Gizmos.color = Color.white;
        Gizmos.DrawWireSphere(transform.position, 0.5f);
    }
}

This class looks for a GemManager in the scene when it starts and, assuming it does, it asks it for a random gem definition and stores that in its GemDefinition property. We also added an OnDrawGizmos() so we could see, in the Scene View, where the gems have been scattered.

Updating ItemScatterer

All we needed to do was to set Item in the to new random gem prefab and removing ActivateNearest(), as RandomGem doesn’t have Activate()/Deactivate() functions, which our lamp post did.

Adding a New Player Action: Digging

To see the existing user actions that had been defined, we navigated to the Player Capsule object and looked at the PlayerInput component. It references an asset called StarterAsset which, when we double-click on it, opens a new editor window. We use the “+” button next to Actions to defined a new action called “Dig”. Next to “Dig” we use the “+” to defined two new bindings; one to the ‘X’ key on the keyboard and one to the “Button West” button on the gamepad.

To respond to this new action, we create a new script called PlayerDig.cs, containing a function called OnDig(), and attached it to the “Player Capsule” game object – it has to be on the same game object as the PlayerInput component. Here’s the code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerDig : MonoBehaviour
{
    public void OnDig()
    {
        Debug.Log("Player hit dig");
    }
}

In Play mode, we can see now that every time we hit the “X” key the message “Player hit dig” is written to the console.

Creating a Dust Particle Effect

On our Teams site there is a small image file called “dustmote_alpha.png”. We added it to our project in the ‘Sprites’ folder. Selecting it, we updated the properties in the inspector and selected “Alpha is transparency” (to indicate that the image has transparent portions) and hit the “Apply” button to save the change.

We created a new material, in our “Materials” folder, called “Dust Mote”. We needed to change the rendering mode to “Cutout” and assign our “dustmote_alpha.png” image to the texture box for the Albedo channnel:

We then created a new prefab with a particle effect on it. The settings are too numerous to list here, but you should examine it in the project if interested. Our material above provides the appearance of the particles.

We also added an audio source to this prefab and downloaded a digging sound effect from our Teams site to attach to it.

Finally we developed a small class called SelfDestruct.cs and attached it to the prefab:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SelfDestruct : MonoBehaviour
{
    public float SelfDestructInSeconds;

    // Start is called before the first frame update
    void Start()
    {
        Destroy(gameObject, SelfDestructInSeconds);   
    }
}

This little component means that we can create the dust effect and it will clean itself up after the sound and the particle effect have finished (about 3 seconds).

Code Download

The code for this week’s project is on our GitHub, as always. 

Modellers – Week 13

This week we had a 3D printer in the room, so our plans to stencil paint the apple took a back seat until next week. Instead we printed a dice model and then looked at how to build that model.

The printer we used is a Prusa i3 Mk 2.5. To print a model you import it into a program called a slicer which converts it from a polygon based model into instructions for the printer in how to lay down a series of layers of plastic to build the same approximate shape.

The model we printed was a dice. To do this we used ‘hard surface modelling’ techniques, specifically the use of boolean operators. Boolean operators allow you to take two shapes and make a composite shape that is:

  1. Difference: The first shape with the second cut-out
  2. Union: A shape which is the two shape fused together
  3. Intersect: A shape which is only where the two original shapes overlapped

This technique is powerful, but it results in many N-gons (polygons with more than four sides). N-gons are bad in many circumstances. For example severe distortion may result if :

  1. If we try to apply smooth shading
  2. If we try to apply a subsurface modifier
  3. We later try to distort the mesh, as with an animation
  4. If we export the model for use in other 3D packages

If none of those apply, hard surface modelling can have its uses.

Here is the instruction video for this week:

The dice model file can be found here.

Beginners/Intermediate Scratch – Challenge 13 – Learn about the Raspberry Pi

CDA-S2-Challenge13-RaspberryPi-Demo

This week in CoderDojo Athenry, the other streams joined us at the start, as we began with a demo of the Raspberry Pi, a really interesting low-cost computer that has been brought out in the last year. It only costs about €30 for a basic one, to which you add a power supply (many mobile phone ones work on it), an SD card (instead of a hard drive), a keyboard and mouse, and connect it to your TV to get started.

We saw it running Scratch and Python programs, and a couple of them were passed around the room for everyone to examine.

We took the opportunity to learn about the main components in a desktop PC: how to identify them and what the function of each one is. We also saw the equivalent components in the Raspberry Pi.

After that, we returned to Mesh networking and the Chatty program that we started a couple of weeks ago, that not everyone had finished.

Here are my notes from the day (PDF format): CDA-S2-Challenge13-RaspberryPi-Demo.pdf

Ninjas who had finished the Chatty program and wanted to do more did work on a 2-player Rock-Paper-Scissors program. We didn’t have notes for this, but I showed the code and it is posted here: http://scratch.mit.edu/projects/cdathenry/3077050  (This is code for one of the players. Similar code is needed for the other one.)

If you would like a copy of my slides in PowerPoint format, get in touch via Twitter or the comments.

Intermediate Scratch – Challenge 12 – 2-Player Network Guessing Game

CDA-S2-Challenge12-2Player-GuessingGame

This week in CoderDojo Athenry, we continued to play with networking in Scratch.

As we learned last week, you can set up a Mesh network in Scratch so that Scratch programs running on two different computers can exchange information with each other. This week, we used those ideas in combination with our simple guessing game from Challenge 2, to create a 2-player network guessing game.

Since we have new members who have just joined us in the past week, they could focus on the one-player version while more experienced members could work on the networking aspect.

The big ideas behind this challenge were:

  1. Design of a 2-player game (need two programs with different roles and different code)
  2. Communications on the network using variables
  3. Broadcasts across the network
  4. Loops and Decisions: fundamental coding concepts

Here are my notes from the day (PDF format): CDA-S2-Challenge12-2Player-GuessingGame.pdf

Here are my versions of the game for both players:

  • GuessNumberNetwork: this is the one that plays the ‘guess a number game’ when the other one (the ‘boss’) chooses the number
  • GuessNumberNetwork-Boss: this is the one that allows you choose the number and then communicates with the other one that plays

Note that you must download these (they won’t run on the Scratch website) and enable Mesh networking as described in the notes for them to work.

If you would like a copy of my slides in PowerPoint format, get in touch via Twitter or the comments.

Scratch Advanced – Week 13 – Gravity and Speed

What are we going to do this week?

  • Gravity
  • Speed
  • Momentum

We are going to look at Gravity and how it works here on Earth. We will also be looking at how, when you travel fast enough, you can appear to defy Gravity, this is how Satellites stay in orbit around the Earth.

We will also take a look at momentum and how things behave once they are moving.

And in order to demonstrate the effects of Gravity,  we will build a Spaceship Game, were you have to launch yourself into Orbit.

Depending upon how we get on this may run over two weeks.

Now back to Gravity. If you  want to get all smart, there is a very clever equation that Sir Issac Newton came up with back in 1687. But we won’t go there for now.

Gravity is basically a force of attraction and the closer you get to something the stronger that force is, also the bigger somehting is the more gravitational force it will have.

It’s a very small force, it takes the whole of the planet Earth to hold us down.

So how are we going to show this in Scrartch, well we will start with a Rocket on the Earth, that we have to Launch upwards, Rockets produce Thrust, which pushes them up into the air. The Earth has Gravity which is trying to pull the rocket back down, but the further you travel away from the Earth the weaker the Gravity and so the Less Thrust you need.

I think I just spotted three Variables there…

1. Thrust

2. Gravity

3. Height

They might come in useful in our program.

We are going to try and build a Scratch Game that will launch a rocket into orbit. This mioght take us a couple of weeks, but should be good fun.

So lets try and write down the rules that we need the Game to obey:

1. Pushing the up arrow should fire the rocket and get us moving.

2. If we stop firing the rocket then gravity will start to pull us back down

3. The higher we go the less Gravity can pull us back down.

4. If we travel fast enough, it can make us appear to defy Gravity, that’s how satellites stay in orbit, and the Moon for that matter. But we will leave this out to start off with as it can start to get really complicated 🙂

To start us off I found a Lunar Lander Game that has some of the elements that we need, this will be a good starting point to  get us going.

To start off we only need a simple sprite, a ball with two legs, that should do for our rocket 🙂