官术网_书友最值得收藏!

Disabling objects by distance

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());
}

IEnumerator DisableAtADistance() {
while(true) {
float distSqrd = (transform.position - _target.transform.position).sqrMagnitude;
if (distSqrd < _maxDistance * _maxDistance) {
enabled = true;
} else {
enabled = false;
}

for (int i = 0; i < _coroutineFrameDelay; ++i) {
yield return new WaitForEndOfFrame();
}
}
}

We should assign the player's character object (or whatever object we want it to compare with) to the _target field in the Inspector window, define the maximum distance in _maxDistance, and modify the frequency with which the coroutine is invoked using the _coroutineFrameDelay field. Anytime the object goes further than _maxDistance distance away from the object assigned to _target, it will be disabled. It will be re-enabled if it returns within that distance.

A subtle performance-enhancing feature of this implementation is comparing against distance-squared instead of the raw distance. This leads us conveniently to our next section.

主站蜘蛛池模板: 漯河市| 吴旗县| 萨迦县| 历史| 扶风县| 宾阳县| 兴和县| 烟台市| 北辰区| 泰安市| 庆城县| 西丰县| 汝南县| 兴海县| 交口县| 济阳县| 临夏县| 寿阳县| 麻城市| 泊头市| 宁都县| 渝北区| 阿拉善右旗| 左贡县| 安吉县| 湖北省| 岐山县| 水城县| 石门县| 巩义市| 拜泉县| 松阳县| 绥化市| 长春市| 达尔| 红桥区| 福泉市| 吴江市| 北京市| 贵定县| 庄河市|