Instantiation
Time 2 hrs

Difficulty Intermediate
Prerequisites Computer Physics
Departments Science
Authors Sandra Kuipers
Groupings Individual
Minimum Year Group None

Blurb

Instantiation.

License

This work is shared under the following license: Creative Commons BY-SA-NC

Outline

Learner Outcomes
Students will:
  • ...
Competency Focus
  • ...
Interdisciplinary Connections
  • ...
Reflection
What was successful? What needs changing? Alternative Assessments and Lesson Ideas? What other Differentiation Ideas/Plans could be used?
  • ...
Credits
Any CC attribution, thanks, credit, etc.
  • ...

This page requires you to be logged in to access it. Please login and try again.
5 mins
Dynamic Content
Getting Started

  • Video games are rarely static!
  • Game levels often have moving parts, bad guys, power-ups, projectiles, or some other kind of dynamic content.
  • Instantiation, meaning to create a new instance of something, is the key to dynamic content.
  • Games use instantiation to build a level before a player starts, as well as while they're playing.
10 mins
Instantiating Objects
Digging In
  • It's easy to pre-build your game in Unity by dragging objects onto the scene.
  • But what if you wanted your game to load endlessly as the player moved?
  • Instantiating allows you to create new objects while your game is running:
public Transform myObject;
void Start()
{
    Instantiate(myObject, new Vector3(0, 0, 0), Quaternion.identity);
}
  • In this example, myObject is being instantiated with an object, a position, and a rotation (Quaternion.identity just means "default rotation").

Instantiate(Object original, Vector3 position, Quaternion rotation); 
public Transform myObject;
void Start()
{
    for (int x = 0; x < 10; x++)
    {
        for (int y = 0; y < 10; y++)
        {
            Instantiate(myObject, new Vector3(x * 2.0F, y * 2.0F, 0));
        }
    }
}
10 mins
Instantiating Prefabs
Digging In
  • Spawning only cubes and spheres doesn't make for a very exciting game.
  • To instantiate something bigger, we can make an object ahead of time and save it as a prefab.
  • Check out the Instantiating Prefabs at run time documentation.
  • Or, if you prefer watching rather than reading, the following video has an example of prefabs in use:

  • Once you've created a prefab, you can use variables to attach it to your script, then instantiate it.

10 mins
Projectiles
Digging In
  • Projectiles are objects that are fired or triggered by an event.
  • Bullets, cannonballs, arrows, and lasers are all projectiles.
  • Firing a projectile is a combination of instantiation plus velocity.

  • The following script shows how to launch a projectile using the Instantiate() function.
  • It can be added as a script to the main character of a game.
  • The line transform.forward * speed will fire the projectile from the direction the script's game object (the character) is currently facing.
using UnityEngine;
public class FireProjectile : MonoBehaviour
{
    public Rigidbody projectile;
    public float speed = 4;
    void Update()
    {
        if (Input.GetButtonDown("Fire1"))
        {
            Rigidbody p = Instantiate(projectile, transform.position, transform.rotation);
            p.velocity = transform.forward * speed;
        }
    }
}
5 mins
Instantiation in Games
Asking Questions
  • Take a moment to consider some of the ways instantiation can be used in games.
  • Some ideas to consider (but there's many more than this!)
    • Building a game level
    • Expanding a level as you move
    • Spawning enemies
    • Firing projectiles
    • Randomly placing power-ups
    • Creating a grid (x and y loops)
    • Placing random obstacles
  • Think about a game like Minecraft, for example. In what ways do you think it makes use of instantiation?

60 mins
Enhancing your Roller Game
Evidence
  • How can you use instantiation to to enhance your roll-a-ball game?
  • Open up your roll-a-ball game from the Physics unit.
  • Try making it more challenging with dynamic content, such as:
    • Adding content as the player moves.
    • Instantiating random objects.
    • Instantiating prefabs during run-time.
    • Adding projectiles.
  • Take some time to work on enhancing your game, then save and upload your project and submit the Google folder link as evidence of your learning in this unit.
20 mins
Play Testing!
Extra
  • What's the fun in making a game if no one plays it!?
  • Once you've finished your game, feel free to ask a friend or classmate to play-test your game.
  • If they've made a game too, you could swap and test each other's game.
There are no records to display.
Powered by Gibbon v28.0.00dev

Founded by Ross Parker at ICHK Secondary | Built by Ross Parker, Sandra Kuipers and the Gibbon community
Copyright © Gibbon Foundation 2010-2024 | Gibbon™ of Gibbon Education Ltd. (Hong Kong)
Created under the GNU GPL | Credits | Translators | Support