using Assets.Code.Scripts; using Obvious.Soap; using UnityEngine; public class FishFinderController : MonoBehaviour { public bool IsEnabled = true; public float RefreshRate = 1f; public FloatVariable SonarDeepSetting; public FloatVariable CurrentSonarDepth; [SerializeField] private SonarViewObject _fishView; [SerializeField] private SonarViewObject _ground; [SerializeField] private SonarViewObject _water; private float _lastRefreshTime; private Pool _PoolGround; private Pool _PoolFish; private void Awake() { _PoolGround = Pool.CreateInstance(_ground, 15, _ground.transform.parent); _PoolFish = Pool.CreateInstance(_fishView, 50, _fishView.transform.parent); } private void FixedUpdate() { if (_lastRefreshTime <= Time.time - RefreshRate) { CheckGround(); FishCheck(); _lastRefreshTime = Time.time; IsEnabled = true; } else { IsEnabled = false; } } private void BackToPoolObject(SonarViewObject viewObject) { _PoolGround.Reclaim(viewObject); viewObject.ResetObject(); } private void CheckGround() { if (Physics.Raycast(base.transform.position, Vector3.down, out var hitInfo, SonarDeepSetting.Value, LayerMask.GetMask("Terrain"))) { SonarViewObject instance = _PoolGround.GetInstance(); instance.gameObject.SetActive(value: true); SetViewObject(instance, hitInfo.distance); CurrentSonarDepth.Value = hitInfo.distance; instance.OnReachOutScreen += BackToPoolObject; } } private void FishCheck() { Collider[] array = Physics.OverlapSphere(base.transform.position, CurrentSonarDepth.Value, LayerMask.GetMask("Fish")); if (array.Length != 0) { SonarViewObject instance = _PoolFish.GetInstance(); instance.gameObject.SetActive(value: true); SetViewObject(instance, Vector3.Distance(base.transform.position, array[0].transform.position)); } } private void SetViewObject(SonarViewObject obj, float distance) { obj.Enabled = true; obj.Depth = distance; } }