Unity Algorithms Flashcards
HIGH PRIORITY
PATHFINDING ALGORITHM
Explanation: Pathfinding algorithms are used to find the shortest path between two points on a grid or in a graph.
Usage: Implementing enemy AI movement, NPC navigation, player movement on a map.
A* (A-star)
A* (A-star) algorithm is popular for pathfinding. Here’s a basic example
// Example A* pathfinding using Unity’s NavMesh system
using UnityEngine;
using UnityEngine.AI;
public class AStarPathfinding : MonoBehaviour
{
public Transform target;
private NavMeshAgent agent;
void Start() { agent = GetComponent<NavMeshAgent>(); if (agent != null && target != null) { agent.SetDestination(target.position); } } }
Sorting Algorithms
Explanation: Sorting algorithms arrange elements of a list in a specific order (e.g., numerical or alphabetical).
Usage: Ordering game objects by distance, sorting high scores, organizing inventory items.
QuickSort
// Example QuickSort implementation
public class QuickSort
{
public static void Sort(int[] array, int left, int right)
{
if (left < right)
{
int pivot = Partition(array, left, right);
if (pivot > 1) Sort(array, left, pivot - 1); if (pivot + 1 < right) Sort(array, pivot + 1, right); } } private static int Partition(int[] array, int left, int right) { int pivot = array[left]; while (true) { while (array[left] < pivot) left++; while (array[right] > pivot) right--; if (left < right) { if (array[left] == array[right]) return right; int temp = array[left]; array[left] = array[right]; array[right] = temp; } else { return right; } } } }
Random Number Generation
Explanation: Algorithms for generating pseudo-random numbers.
Usage: Spawning enemies randomly, generating loot drops, procedural generation of terrain.
Example: Using Unity’s Random class for basic random number generation.
// Example random number generation in Unity
int randomIndex = Random.Range(0, spawnPoints.Length);
Instantiate(enemyPrefab, spawnPoints[randomIndex].position, Quaternion.identity);
Finite State Machines (FSM)
Explanation: Modeling complex behaviors by defining states and transitions between them.
Usage: Implementing AI behavior (e.g., patrolling, attacking, fleeing).
Example: Using enums or classes to represent states and switching between them.
// Example Finite State Machine using enums
public enum EnemyState
{
Patrol,
Chase,
Attack,
Dead
}
public class EnemyController : MonoBehaviour
{
private EnemyState currentState;
void Start() { currentState = EnemyState.Patrol; } void Update() { switch (currentState) { case EnemyState.Patrol: // Implement patrol logic break; case EnemyState.Chase: // Implement chase logic break; case EnemyState.Attack: // Implement attack logic break; case EnemyState.Dead: // Implement death logic break; } } }
Dijkstra’s Algorithm
Explanation: Finds the shortest path in a weighted graph.
Usage: Route finding in non-grid-based environments, such as navigating a network of connected nodes.
Example: Implementing a GPS-like navigation system within a game world.
// Example Dijkstra’s Algorithm for finding shortest path in Unity
public class DijkstraAlgorithm
{
public static List<Node> FindShortestPath(Node start, Node goal)
{
// Implementation of Dijkstra's algorithm
// Return the shortest path as a list of nodes
}
}</Node>