0% found this document useful (0 votes)
64 views

Week 4 - Essential Coding Concepts-1

The document discusses essential coding concepts including instantiating and destroying prefabs, invoking methods, vector arithmetic, storing player data, and more. Concepts like instantiation, invocation, prefabs, playerprefs, vectors, magnitude, and normalization are explained.

Uploaded by

snaumanb
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views

Week 4 - Essential Coding Concepts-1

The document discusses essential coding concepts including instantiating and destroying prefabs, invoking methods, vector arithmetic, storing player data, and more. Concepts like instantiation, invocation, prefabs, playerprefs, vectors, magnitude, and normalization are explained.

Uploaded by

snaumanb
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Essential Coding Concepts-1

Week 4
Course Title: Game Development and Modeling
Instructor: Nauman Ahmed
Email: [email protected]
• Instantiate
• Invoke
• Prefab Instantiate
• Prefab destroy
• Playerprefs and data storage
• Understanding Vector Arithmetic
• Vector Distance and Direction
• Magnitude of a Vector
• Vector Normalization
Instantiate

• Instantiate can be used to create new


objects at runtime.
– Examples include objects used for projectiles,
or particle systems for explosion effects.
– public static Object Instantiate(Object original);
– public static Object Instantiate(Object original, Transform
parent);
– public static Object Instantiate(Object original, Transform
parent, bool instantiateInWorldSpace);
– public static Object Instantiate(Object original, Vector3
position, Quaternion rotation);
– public static Object Instantiate(Object original, Vector3
position, Quaternion rotation, Transform parent);
Invoke

• Invokes the method methodName in time


seconds.
– If time is set to 0, the method is invoked at the
next Update cycle. In this case, it's better to
call the function directly.

Invoke("LaunchProjectile", 2.0f);
Prefab

• Unity’s Prefab system allows you to create,


configure, and store a GameObject
complete with all its components, property
values, and child GameObjects as a
reusable Asset.
– The Prefab Asset acts as a template from
which you can create new Prefab instances in
the Scene
– When you want to reuse a GameObject
configured in a particular way – like a non-
player character (NPC), prop or piece of sce
Prefab Instantiate
using UnityEngine;
public class InstantiationExample : MonoBehaviour
{
// Reference to the Prefab. Drag a Prefab into this field in the Inspector.
public GameObject myPrefab;

// This script will simply instantiate the Prefab when the game starts.
void Start()
{
// Instantiate at position (0, 0, 0) and zero rotation.
Instantiate(myPrefab, new Vector3(0, 0, 0), Quaternion.identity);
}
}
Prefab destroy
using UnityEngine;
public class InstantiationExample : MonoBehaviour
{
// Reference to the Prefab. Drag a Prefab into this field in the Inspector.
public GameObject myPrefab;

// This script will simply instantiate the Prefab when the game starts.
void Start()
{
// Instantiate at position (0, 0, 0) and zero rotation.
GameObject g = Instantiate(myPrefab, new Vector3(0, 0, 0),
Quaternion.identity);
Destroy(g,.5f);
}
}
Playerprefs and data storage
• PlayerPrefs is a useful built-in utility that makes it easy to
persist certain types of data between scenes.
– PlayerPrefs is accessed like a hashtable or dictionary.
– You can use the included SetString(), SetInt(), or SetFloat()
methods to set a key/value pair.

void SetLanguagePreference(string language)


{
PlayerPrefs.SetString(“language”, language);
}

void SetDifficultyPreference(string difficulty)


{
PlayerPrefs.SetString(“difficulty”, difficulty);
}
Understanding Vector Arithmetic
• Addition

• Subtraction

• Dot Product
Understanding Vector Arithmetic
• Cross Product
Vector Distance and Direction
• The length of the line shows its magnitude and the arrowhead
points in the direction.
• And it doesn't matter which order we add them, we get the same
result
Magnitude of a Vector
• |a| = √( x2 + y2 )

• Example: what is the magnitude of the vector b = (6,8) ?


– |b| = √( 62 + 82) = √( 36+64) = √100 = 10
Vector Normalization
• Normalization consists of dividing every entry in a vector by its
magnitude to create a vector of length 1 known as the unit vector

You might also like