Creators – Week 7

This week we continued our Prototype 2 project.

Shooting Pizza

The first change we made was have it so that the pizza slices only emanated from the player when the space bar was pressed. We removed the temporary SpawnOnInterval component from the Player as this just spawned pizza slices continuously. We edited PlayerController.cs and added a new public property:

public GameObject projectilePrefab;

This has a type of GameObject which means it can store a reference to an object in the scene, or to a prefab, which is what we want. In the Inspector, we assigned our pizza slice prefab to this property.

In the Update() function in PlayerController.cs we added the following at the end of the function (just above the final curly brace that marks the end of the function):

        if (Input.GetKeyDown(KeyCode.Space))
        {
            // Launch a projectile from the player
            Instantiate(projectilePrefab, transform.position, transform.rotation);
        }

This checks to see if the space bar is pressed and, if it is, then it creates an “instance” (copy) of the pizza slice and places it at the player’s location with the player’s rotation.

Since the pizza slice already knows how to fly forwards, once it’s created it will automatically fly up the screen.

Creating Animal Prefabs

We have three animals in our scene. We checked to make sure that they had been rotated 180 degrees, so that the were pointing down the screen. We dragged each one from the Heirarchy to our Prefabs folder in turn, and then removed them from the scene.

In the prefabs folder, we selected them one at a time and added the MoveForwards.cs script in the Inspector, adjusting the speed from the default of 40 to a more sedate 5.

To test this, we ran the game with the Scene and Game views side-by-side. We could drag and drop animal prefabs into the Scene view and watch them run down the screen. Note that because we added them in run-mode, once the game was stopped, they were not left permanently in the scene.

Keeping Things Tidy

At the moment, any pizza slices and animals added to the scene are there for ever, long after they’ve moved off screen. In any game keeping stuff around that we don’t need any more is wasteful and might mean the game became ever slower as we continued to play it.

We create a new script in our Scripts folder called DestroyOutOfBounds.cs. We give it one private property:

private float topBound = 30;

This represents how far up something can travel in the game before we should delete it. Given our current game camera, up on the screen is equivalent to the global Z axis.

In Update() we add the following code:

        if (transform.position.z > topBound)
        {
            Destroy(gameObject);
        }

What does this say? It says: if we have moved such that our position in Z is greater than 30 (topBound) we should call Destroy(gameObject). The property “gameObject” hold a reference to the item in the scene that this component is attached to. Destroy() removes it from the scene.

We attach this new script to our pizza slice prefab and test it. Now pizza slices don’t stay in the scene forever, when they move to the top of the screen, they are removed.

What about the animals? We can use this same script for them as well. We add another private property:

   private float lowerBound = -10;

This represents how far down the screen things can move before they’re removed.

We change Update() so that it now looks like this:

        if (transform.position.z > topBound)
        {
            Destroy(gameObject);
        }
        else if (transform.position.z < lowerBound)
        {
            Destroy(gameObject);
        }

We have the earlier check for things going out the top, but now we have else if and another check for things going out the bottom. Note that with else if the second thing won’t be checked if the first thing turns our to be true, only when it isn’t.

Adding this now to our animal prefabs as well, we can see that when we drag them into the scene during runtime, they only last until the bottom of the screen.

Spawning Animals

We added a new empty object to the scene and called it “Spawn Manager”. We then created a new script file called SpawnManager.cs and attached it to this object.

In SpawnManager.cs we added a new property:

public GameObject[] animalPrefabs;

This property has the type GameObject, so it can store references to items in the scene, or prefabs. Note the square brackets after the type. This means this isn’t a normal property that stores a single value, this is an array and it can hold multiple values all at the same time. Looking at it in the Inspector, we find we can set its size to three and add all our animal prefabs to it:

We add a second property called animalIndex that will allow us to select which individual prefab we’re talking about at any one time:

public int animalIndex;

We can then add this code to Update():

if (Input.GetKeyDown(KeyCode.S))
{
  Instantiate( animalPrefabs[animalIndex],
                     new Vector3(0, 0, 20),
                     animalPrefabs[animalIndex].transform.rotation );
}

Every time we press the S key, an animal is spawned at the position of (0, 0, 20) (i.e. centre-top of the screen). By changing the value of animalIndex in the inspector between 0, 1 and 2 we can see different animals get spawned.

Random Animals in Random Places

We enhanced our SpawnManager.cs by randomising both the which animal is spawned and where it’s spawned. At the top of the class, we delete the animalIndex property and add these private properties instead:

    private float spawnRangeX = 20;
    private float spawnPosZ = 20;

The first we’ll use to determine the position of the animal horizontally, a random number between the left and right sides of the screen. The second will be the animals vertical position, and this will always be the same.

Inside Update() we change it to look like this:

int animalIndex = Random.Range(0, animalPrefabs.Length);
Vector3 spawnPos = new Vector3(Random.Range(-spawnRangeX, spawnRangeX),
                                       0, spawnPosZ);

if (Input.GetKeyDown(KeyCode.S))
{
  Instantiate( animalPrefabs[animalIndex],
                     spawnPos,
                     animalPrefabs[animalIndex].transform.rotation );
}

Now we’re setting animalIndex automatically every time to a random integer between 0 and up-to, but not including, animalPrefabs.Length. By using animalPrefabs.Length we can ensure this code works, regardless of how many animal prefabs we add to SpawnManager.

The position we’re also creating as a variable called spawnPos. The X value is random float value between -spawnRangeX and spawnRangeX. Y is aways zero and Z is always spawnPosZ. Note that down in Instantiate() we’re now using spawnPos and not the former new Vector3(0, 0, 20).

Automatic Spawning

The final change is automatic spawning. We move all the spawning code to a new function called “SpawnRandomAnimal”, where the check for the key press has been removed:

void SpawnRandomAnimal()
{
    int animalIndex = Random.Range(0, animalPrefabs.Length);
    Vector3 spawnPos = new Vector3(Random.Range(-spawnRangeX, spawnRangeX),
                                   0, spawnPosZ);

    Instantiate(animalPrefabs[animalIndex],
                spawnPos,
                animalPrefabs[animalIndex].transform.rotation);
}

Now all we need is to call this automatically. We create a new pair of private properties:

    private float spawnDelay = 2;
    private float spawnInterval = 1.5f;

and add this to the Start() function:

       InvokeRepeating("SpawnRandomAnimal", spawnDelay, spawnInterval);

This function will wait spawnDelay seconds, then call the named function for the first time. It will then call it repeatedly after that every spawnInterval seconds.

Now when we run, animals getting created constantly at random! No more need to press S.

And Finally…

The code for this week’s project is on our GitHub, as always. To download our stuff from GitHub, you can just click on the green button and choose “Download ZIP”

Note that this ZIP file will contain all our projects for the year!

Creators – Week 2

Hi folks, this week we started our first project. We have a truck that can drive down a road, avoiding obstacles, or maybe not!

We created a new project in Unity, using the default 3D Core template, and called it Prototype 1.

We then downloaded and imported the assetpack from here: [Creators Teams Channel]

The asset pack already included an existing scene, which had a simple environment already. We them dragged in a vehicle and obstacle from the imported assets. Imported assets aren’t just models; they can contain Unity obstacles, such as colliders, already.

To make the truck move, we made a new C# script called PlayerController. The new C# files Unity creates always look the same (apart from the name of the Class, which matches the new file name):

We added the following code to the Update() method to change the transform of the vehicle:

    // Update is called once per frame
    void Update()
    {
        //  Move our vehicle forward
        transform.Translate(0, 0, 1);
    }

The Unity scripting documentation can be found:

https://docs.unity3d.com/ScriptReference/index.html

and the specific page for the Transform component is:

https://docs.unity3d.com/ScriptReference/Transform.html.

This method on the Transform component that we’re calling, Translate() has several forms. The one we’re using here expects us to provide X, Y, Z values. What we’re saying we want to happen is “Change the transform by moving it 1m in Z every frame.

When we run, the car moves very fast off the end of the road. That’s because we’re running at many frames a second. It’s too fast. Next week, we’ll look at making this frame rate independent and controlling the speed.

Finally, I’ve created a GitHub repo for our projects this year. Up-to-date versions of our projects will always be available here after our sessions: https://github.com/coderdojoathenry/Creators-2022

CoderDojo Athenry Returns on 01 October 2022!

poster 2 2022

CoderDojo Athenry is returning with our weekly sessions in Clarin College starting Saturday 01 October 2022.

Our sessions will take place between 2:00pm and 4:00pm.

We will run five groups this year Explorers, Advancers, Bodgers, Creators and Hackers.

There is more information about the groups, our location and everything else on our About page.

New members are always welcome. If you are aged between 7 and 17, just come along on the first day and fill out a registration form. Young people aged 12 and under have to be accompanied by a parent/guardian for the whole session.

And don’t forget, CoderDojo Athenry is run by volunteers and is completely free for participants — no membership fees, no weekly contributions.

You should bring a laptop if you have one, but we have some loaner laptops if you don’t.

Explorers

No code for this weeks session, as we will be having a party!!

We will supply food, but please BRING YOUR OWN DRINKS.

As each family has there own policy on fizzy drinks, sugar, caffeine etc it is best if everyone brings their own.

See you all Saturday.