diff --git a/Assets/02.Scripts/Prop/Rockfall.cs b/Assets/02.Scripts/Prop/Rockfall.cs index 85b26803b..f285a9527 100644 --- a/Assets/02.Scripts/Prop/Rockfall.cs +++ b/Assets/02.Scripts/Prop/Rockfall.cs @@ -1,3 +1,4 @@ +using System; using System.Collections; using BlueWater.Audios; using BlueWater.Interfaces; @@ -45,66 +46,86 @@ namespace BlueWater private ParticleSystem _groundCrashParticle; private Collider[] _hitColliders = new Collider[4]; + private RaycastHit _raycastHit; + private float _startDistance = float.PositiveInfinity; private bool _isGrounded; private bool _isAttacked; // Hashes private static readonly int _fillHash = Shader.PropertyToID("_Fill"); - private IEnumerator Start() + private void OnDrawGizmos() + { + Debug.DrawRay(transform.position, Vector3.down * _checkDistance, _isGrounded ? Color.blue : Color.red); + } + + private void Start() { _sphereCollider.enabled = false; SpawnLocation = MapManager.Instance.SandMoleMapController.ParticleInstantiateLocation; + + if (!Physics.Raycast(transform.position, Vector3.down, out _raycastHit, 10f, _groundLayer)) + { + Debug.LogError("땅을 감지하지 못하는 버그"); + return; + } + BasicSetting(); ShowIndicator(); + _startDistance = _raycastHit.distance; + } - var startDistance = float.PositiveInfinity; - while (!_isGrounded) + private void Update() + { + if (!Physics.Raycast(transform.position, Vector3.down, out _raycastHit, 10f, _groundLayer)) { - if (!Physics.Raycast(transform.position, Vector3.down, out var hit, 10f, _groundLayer)) continue; - - if (float.IsPositiveInfinity(startDistance)) - { - startDistance = hit.distance; - } + Debug.LogError("땅을 감지하지 못하는 버그"); + return; + } + _isGrounded = _raycastHit.distance <= _checkDistance; + if (!_isGrounded) + { if (_isUsingIndicator && _indicator) { - var fillValue = Mathf.Lerp(1f, 0f, hit.distance / startDistance); + var fillValue = Mathf.Lerp(1f, 0f, _raycastHit.distance / _startDistance); _indicator.material.SetFloat(_fillHash, fillValue); } - _isGrounded = hit.distance <= _checkDistance; - yield return null; } - - _indicator.material.SetFloat(_fillHash, 1f); - HideIndicator(); - - if (_rigidbody) + else if (_isGrounded && !_isAttacked) { - _rigidbody.isKinematic = true; - } - _sphereCollider.enabled = true; - - if (!string.IsNullOrEmpty(_groundCrashSfxName)) - { - AudioManager.Instance.PlaySfx(_groundCrashSfxName); - } - - if (_groundCrashParticle && SpawnLocation) - { - Instantiate(_groundCrashParticle, transform.position, Quaternion.identity, SpawnLocation); - } - - var hitCount = Physics.OverlapSphereNonAlloc(_sphereCollider.bounds.center, _sphereCollider.radius, - _hitColliders, _targetLayer, QueryTriggerInteraction.Collide); - for (var i = 0; i < hitCount; i++) - { - var hitCollider = _hitColliders[i]; - var iDamageable = hitCollider.GetComponentInParent(); - if (iDamageable == null || !iDamageable.CanDamage()) continue; + _indicator.material.SetFloat(_fillHash, 1f); + HideIndicator(); - iDamageable.TakeDamage(_attackDamage); + if (_rigidbody) + { + _rigidbody.isKinematic = true; + } + _sphereCollider.enabled = true; + + if (!string.IsNullOrEmpty(_groundCrashSfxName)) + { + AudioManager.Instance.PlaySfx(_groundCrashSfxName); + } + + if (_groundCrashParticle && SpawnLocation) + { + Instantiate(_groundCrashParticle, transform.position, Quaternion.identity, SpawnLocation); + } + + var hitCount = Physics.OverlapSphereNonAlloc(_sphereCollider.bounds.center, _sphereCollider.radius, + _hitColliders, _targetLayer, QueryTriggerInteraction.Collide); + for (var i = 0; i < hitCount; i++) + { + var hitCollider = _hitColliders[i]; + var iDamageable = hitCollider.GetComponentInParent(); + if (iDamageable == null || !iDamageable.CanDamage()) continue; + + iDamageable.TakeDamage(_attackDamage); + Die(); + } + + _isAttacked = true; } }