Week 4 - Essential Coding Concepts-1
Week 4 - 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
Invoke("LaunchProjectile", 2.0f);
Prefab
// 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.
• 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 )