Skip to content

Instantly share code, notes, and snippets.

@charlieamat
Last active September 14, 2021 15:55
Show Gist options
  • Save charlieamat/ef013c588a1ed4ad13e242357c267555 to your computer and use it in GitHub Desktop.
Save charlieamat/ef013c588a1ed4ad13e242357c267555 to your computer and use it in GitHub Desktop.
[ta-edu-course-survival-game] Chapter 2—Performance 101 (2)
  1. Comment out the existing logic (except for _nearestInteractable)
    • We’re going to re-write Interactor using under-performant code
  2. Implement the Update method
    • Instead of reacting to GameObjects that invoke the “OnTrigger” methods, we’re going to poll the scene for interactables
  3. In Update, declare and initialize a variable called “interactables” using FindObjectsOfType()
    • Like the “GetComponent” methods, Unity has a set of “Find” methods that allow you to search the scene for GameObjects
    • These are generally unsafe because you can’t always predict how many objects will actually be in your scene
  4. Add a foreach that loop that iterates over interactables
  5. Check if _nearestInteractable is null:
    • If it is, set it to the current interactable
    • Otherwise, check the distance between the _nearestInteractable and the current intractable and update _nearestInteractable if it’s closer
  6. Switch to Unity
  7. Add 100 interactors and 500 interactables to the scene
    • You can lower these numbers if your computer is having trouble running this scene
    • Let’s pretend that we have a large world with 500 interactables
    • We can also assume that we’ll let actors outside of the player interact with the world, so we’ll create 100 interactors, too
  8. Run your game, wait a few seconds, and press the pause button
    • Now our scripts are taking much longer to process
public class Interactor : MonoBehaviour
{
private Interactable _nearestInteractable;
private void OnInteract() => Interact();
private void Interact()
{
if (!_nearestInteractable) return;
_nearestInteractable.Collect(gameObject);
}
private void OnTriggerEnter(Collider other)
{
var collectable = other.GetComponent<Interactable>();
if (!collectable) return;
_nearestInteractable = collectable;
}
private void OnTriggerExit(Collider other)
{
var collectable = other.GetComponent<Interactable>();
if (!collectable || _nearestInteractable != collectable) return;
_nearestInteractable = null;
}
}
using UnityEngine;
public class Interactor : MonoBehaviour
{
private Interactable _nearestInteractable;
public OnInteract() => Interact();
public void Interact()
{
if (!_nearestInteractable) return;
_nearestInteractable.Interact();
}
public void Update()
{
var interactables = FindObjectsOfType<Interactable>();
foreach (var interactable in interactables)
{
if (_nearestInteractable == null)
{
_nearestInteractable = interactable;
}
else
{
var distToNearest = Vector3.Distance(transform.position, _nearestInteractable.transform.position);
var distToCurrent = Vector3.Distance(transform.position, interactable.transform.position);
if (distToCurrent < distToNearest)
{
_nearestInteractable = interactable;
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment