using System.Collections.Generic; using UnityEngine; public class WallHider : MonoBehaviour { public Transform lastWall; List walls = new List(); float directionThreshold = 0; public void CheckIfCameraIsBehindWall() { if (walls.Count <= 0) { for (int i = 0; i < _G.NW; i++) { string wallName = "w" + (i + 1).ToString(); Transform wall = SceneModeManager.GetObjectInRoom(wallName, true); if (wall != null) { walls.Add(wall.transform); } } } float currentClosestDotToWall = 0; Transform currentWall = null; if (walls.Count > 0) { // Go through all the wall in the walls list foreach (Transform wall in walls) { if(wall == null) { walls.Clear(); return; } //if (wallThatCameraIsBehind == wall) { Debug.Log($"SAME WALL{wall} "); continue; } Vector3 wallToCamera = transform.position - wall.position; //In normal circumstances, wallToCamera should not be negative, but our wall are inverted. float dotProduct = Vector3.Dot(wallToCamera.normalized, wall.forward); // if dot product is negative, camera is behind wall. if (dotProduct > 0) // derriere le mur { Vector3 cameraDirection = transform.forward; cameraDirection.y = 0; float dotBetweenCameraAndWall = Vector3.Dot(cameraDirection.normalized, -wall.forward); if (dotBetweenCameraAndWall > directionThreshold) // regarde dans la meme direction { float differenceA = Mathf.Abs(1.0f - currentClosestDotToWall); float differenceB = Mathf.Abs(1.0f - dotBetweenCameraAndWall); if(differenceA > differenceB) { currentClosestDotToWall = dotBetweenCameraAndWall; currentWall = wall; } } } } //unhide all object on wall before getting new wall if (lastWall != null && currentWall != lastWall) { ToggleObjectsOnSelectedWall(lastWall, true); } lastWall = currentWall; //hide object on new wall ToggleObjectsOnSelectedWall(currentWall, false); } } private void ToggleObjectsOnSelectedWall(Transform wall, bool toggle) { if (wall == null) return; Transform scene = SceneModeManager.Scene; if (scene.childCount != 0) { foreach (Transform child in scene) { if (child.TryGetComponent(out MoveObject moveObject)) { if (moveObject.AttachedTo == wall) { //HideCommand hide = new HideCommand(child.name, toggle); //hide.Execute(); CameraAround.SetObjectVisibility(moveObject.gameObject, toggle); } } } } } }