ufceku-20-3web games programming instantiating world objects

15
UFCEKU-20-3 Web Games Programming Web Games Programming Instantiating World Objects

Upload: elfreda-jordan

Post on 20-Jan-2018

216 views

Category:

Documents


0 download

DESCRIPTION

UFCEKU-20-3Web Games Programming Instantiating New Objects Instantiation is simply creating objects from a template during runtime – with Unity - a Prefab The term ‘instantiate’ comes from Object-Oriented Programming (OOP) terminology where a new instance of a class is said to be ‘instantiated’ It can also be used to duplicate world objects already in the scene Instantiating new objects while the application is running is also often referred to as ‘spawning’

TRANSCRIPT

Page 1: UFCEKU-20-3Web Games Programming Instantiating World Objects

UFCEKU-20-3Web Games Programming

Web Games Programming

Instantiating World Objects

Page 2: UFCEKU-20-3Web Games Programming Instantiating World Objects

UFCEKU-20-3Web Games Programming

Agenda

Instantiating (spawning) Objects Dynamically During Runtime Steps Required to Prepare an Object for Instantiation Techniques to Assist in Positioning the Instantiated Object Dealing with Unwanted Objects

Page 3: UFCEKU-20-3Web Games Programming Instantiating World Objects

UFCEKU-20-3Web Games Programming

Instantiating New Objects

• Instantiation is simply creating objects from a template during runtime – with Unity - a Prefab

• The term ‘instantiate’ comes from Object-Oriented Programming (OOP) terminology where a new instance of a class is said to be ‘instantiated’

• It can also be used to duplicate world objects already in the scene

• Instantiating new objects while the application is running is also often referred to as ‘spawning’

Page 4: UFCEKU-20-3Web Games Programming Instantiating World Objects

UFCEKU-20-3Web Games Programming

Preparing an Object for Instantiation

• Create the object to instantiate in the scene adding components as required

• Create a new Prefab in the project and drop the object created for instantiation onto the Prefab

• Delete the original object from the scene so it is only stored as a Prefab asset

• Create a script that uses the Instantiate() command together with a variable of type GameObject, attach it to an active world object

• Set the Prefab created as the object the Instantiate command creates by dropping it onto the GameObject variable in the Inspector Window

Page 5: UFCEKU-20-3Web Games Programming Instantiating World Objects

UFCEKU-20-3Web Games Programming

Instantiate Command

• The Instantiate command has three parameters (arguments)• Object to create, position to create it, rotation to give it:

Instantiate(object,object’s position,object’s rotation);

Page 6: UFCEKU-20-3Web Games Programming Instantiating World Objects

UFCEKU-20-3Web Games Programming

Instantiate Command

• Position and Rotation of objects to be instantiated must be specified as Vector3 values (X,Y,Z) used as follows:

var aPrefab: GameObject;// variable of type GameObject

Instantiate(aPrefab, Vector3(0,12,30), Vector3(0,0,90));

Page 7: UFCEKU-20-3Web Games Programming Instantiating World Objects

UFCEKU-20-3Web Games Programming

Position and Rotation

• The position and rotation of objects to be instantiated can also be inherited from other objects via the inherited objects transform.position, and transform.rotation command

• This easiest method is to use a ‘dummy object’ positioned in the world where the instantiate object needs to be created. Then inherit the dummy object’s position and rotation values for the new instantiated object.

• The dummy object mesh renderer can be switched off so the dummy object is invisible in the scene

Page 8: UFCEKU-20-3Web Games Programming Instantiating World Objects

UFCEKU-20-3Web Games Programming

Dummy Object Positioned in Scene at (0, 2, 5)

Page 9: UFCEKU-20-3Web Games Programming Instantiating World Objects

UFCEKU-20-3Web Games Programming

Dummy Object Mesh Render Switched Off

Page 10: UFCEKU-20-3Web Games Programming Instantiating World Objects

UFCEKU-20-3Web Games Programming

Instantiate.js attached to InstantiateCube

// prefab name in project

var ballInstance : GameObject;

function Start () {

// creates one ballInstance at the position of InstantiateCube

Instantiate(ballInstance, transform.position, transform.rotation);

}

Page 11: UFCEKU-20-3Web Games Programming Instantiating World Objects

UFCEKU-20-3Web Games Programming

Removing Objects from the Scene

• Creating many clones of an object can adversely affect system performance

• Stray objects may also create unwanted collisions with other world objects

• Use the Destroy command to remove unwanted objects from the world

• The Destroy command is included in a script attached to the Prefab to be instantiated

Page 12: UFCEKU-20-3Web Games Programming Instantiating World Objects

UFCEKU-20-3Web Games Programming

balltidy.js attached to ballInstance Prefab

function Start () {

/*

Destroy has two arguments gameObject,

and time in seconds before object is removed

*/

Destroy(gameObject,5); // destroys object after 5 seconds has elapsed

}

Page 13: UFCEKU-20-3Web Games Programming Instantiating World Objects

UFCEKU-20-3Web Games Programming

New Ball Prefab Created (Cube Visible)

Page 14: UFCEKU-20-3Web Games Programming Instantiating World Objects

UFCEKU-20-3Web Games Programming

New Ball Prefab Created (Cube Hidden)

Page 15: UFCEKU-20-3Web Games Programming Instantiating World Objects

UFCEKU-20-3Web Games Programming

Summary : Easy Instantiation

• Create the object to to be instantiate as a Prefab

• Attach a script with the Destroy() command to the Prefab

• Create and position a dummy object in the world

• Attach a script to the dummy object with the Instantiate() command and a GameObject variable

• Drag and drop the Prefab onto the dummy object script’s GameObject variable in the Inspector