voidStart() { // dynamicly finding it -> because it is singleton? _input = PlayerInput.GetInstance(); // this is finding the component attached to the same game object _characterController = GetComponent<CharacterController>(); }
public Action<float> OnHealthUpdated; // this means onHealthUpdated func will take float as parameter public Action OnDeath; // this does not take parameter
OnHealthUpdated += AnotherFunc_OnHealthUpdate;
voidAnotherFunc_OnHealthUpdate(float health){ // actions // can be different script }
publicclassSomeClass { //Here is a generic method. Notice the generic //type 'T'. This 'T' will be replaced at runtime //with an actual type. public T GenericMethod<T>(T param) { return param; } }
//In order to use this method you must //tell the method what type to replace //'T' with. myClass.GenericMethod<int>(5); } }
Collision / Trigger
OnCollisionEnter
OnCollisionEnter is called when this collider/rigidbody has begun touching another rigidbody/collider.
OnCollisionExit
OnCollisionExit is called when this collider/rigidbody has stopped touching another rigidbody/collider.
OnCollisionStay
OnCollisionStay is called once per frame for every Collider or Rigidbody that touches another Collider or Rigidbody.
OnTriggerEnter
When a GameObject collides with another GameObject, Unity calls OnTriggerEnter.
OnTriggerExit
OnTriggerExit is called when the Collider other has stopped touching the trigger.
OnTriggerStay
OnTriggerStay is called almost all the frames for every Collider other that is touching the trigger. The function is on the physics timer so it won’t necessarily run every frame.
UnityEvents and Actions are Unity-specific implementations of the Observer pattern. ● AddListener and RemoveListener are the subscription functions. ● Invoking the Action/UnityEvent is the notify behavior.
One of the most common algorithms used for finding the shortest path in graphs is Dijkstra’s algorithm. Here’s a simplified explanation of how it works:
Start by assigning a tentative distance value to every vertex in the graph. Set the distance of the source vertex to 0 and all other vertices to infinity. Mark the source vertex as the current vertex. For the current vertex, consider all of its unvisited neighbors and calculate their tentative distances by summing the distance of the current vertex and the weight of the edge between them. If the calculated tentative distance of a neighbor is less than its current assigned distance, update the neighbor’s distance value. Once all the neighbors of the current vertex have been considered, mark the current vertex as visited and select the unvisited vertex with the smallest tentative distance as the next current vertex. Repeat steps 3-5 until the destination vertex is marked as visited or there are no more unvisited vertices. The shortest path can then be reconstructed by backtracking from the destination vertex to the source vertex using the recorded distances and visited vertices. Dijkstra’s algorithm guarantees that the shortest path is found, given that the graph doesn’t contain negative weight cycles. It explores vertices in a greedy manner, always selecting the vertex with the smallest tentative distance as the next current vertex.
// Function to perform Quick Sort voidQuickSort(int[] array, int low, int high) { if (low < high) { // Partition the array and get the pivot index int pivotIndex = Partition(array, low, high);
// Recursively sort the subarrays before and after the pivot QuickSort(array, low, pivotIndex - 1); QuickSort(array, pivotIndex + 1, high); } }
// Function to partition the array and find the pivot index intPartition(int[] array, int low, int high) { int pivot = array[high]; // Choose the last element as the pivot int i = low - 1;
for (int j = low; j < high; j++) { if (array[j] < pivot) { i++; // Swap elements at indices i and j int temp = array[i]; array[i] = array[j]; array[j] = temp; } }
// Place the pivot in its correct position int pivotIndex = i + 1; int temp2 = array[pivotIndex]; array[pivotIndex] = array[high]; array[high] = temp2;