November 2019
Intermediate to advanced
404 pages
12h 9m
English
In other situations, we may want components or GameObjects to be disabled after they are far enough away from the player such that they may be barely visible, but too far away to matter. A good candidate for this type of activity is roaming Non-Player Character creatures: we want to see them at a distance, but where we don't need them to process anything, and therefore they can sit idle until we get closer.
The following code is a simple coroutine that periodically checks the total distance from a given target object and disables itself if it strays too far away from it:
[SerializeField] GameObject _target;[SerializeField] float _maxDistance;[SerializeField] int _coroutineFrameDelay;void Start() { StartCoroutine(DisableAtADistance()); ...Read now
Unlock full access