using System.Collections.Generic; using UnityEngine; public class HideWall : MonoBehaviour { [SerializeField] private GameObject target; [SerializeField] private GameObject origin; [Range(1f, 10f)] public float hideFadeSpeed; [Range(1f, 10f)] public float showFadeSpeed; public float sphereRadius; public Vector3 maxSphereRayDistanceOffset; float maxSphereRayDistance; private List prevHitObjects = new List(); private void Start() { maxSphereRayDistance = Vector3.Distance(origin.transform.position, target.transform.position) - (sphereRadius * 2 + 1); } void Update() { Vector3 direction = ((target.transform.position + maxSphereRayDistanceOffset) - origin.transform.position).normalized; RaycastHit[] hits = Physics.SphereCastAll(origin.transform.position, sphereRadius, direction, maxSphereRayDistance); List hitObjectsThisFrame = new List(); for (int i = 0; i < hits.Length; i++) { if (hits[i].collider != null && hits[i].collider.CompareTag("Wall")) { var hitGameObject = hits[i].collider.gameObject; hitObjectsThisFrame.Add(hitGameObject); var wallOpacity = hitGameObject.GetComponent(); if (wallOpacity != null && !prevHitObjects.Contains(hitGameObject)) { wallOpacity.HideWall(hideFadeSpeed); } } } foreach (var prevHitObject in prevHitObjects) { if (!hitObjectsThisFrame.Contains(prevHitObject)) { var wallOpacity = prevHitObject.GetComponent(); if (wallOpacity != null) { wallOpacity.ShowWall(showFadeSpeed); } } } prevHitObjects = hitObjectsThisFrame; } private void OnDrawGizmos() { Gizmos.color = Color.red; Gizmos.DrawWireSphere(origin.transform.position, sphereRadius); } }