- Comment out the existing logic (except for _nearestInteractable)
- We’re going to re-write Interactor using under-performant code
- Implement the Update method
- Instead of reacting to GameObjects that invoke the “OnTrigger” methods, we’re going to poll the scene for interactables
- 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
- Add a foreach that loop that iterates over interactables
- 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
- Switch to Unity
- 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
- Run your game, wait a few seconds, and press the pause button
- Now our scripts are taking much longer to process
Last active
September 14, 2021 15:55
-
-
Save charlieamat/ef013c588a1ed4ad13e242357c267555 to your computer and use it in GitHub Desktop.
[ta-edu-course-survival-game] Chapter 2—Performance 101 (2)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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