Hi, i have a problem with this code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RemoveWall : MonoBehaviour
{
private string wallDownTag = "Wall Down";
private string wallLeftTag = "Wall Left";
private string wallUpTag = "Wall Up";
private string wallRightTag = "Wall Right";
public Camera cam;
public GameObject[] removed;
void switchWall(float x, float y)
{
Debug.Log("Switch wall!");
if (x == 90f){
Debug.Log("X = " + x);
Debug.Log("Do not remove wall!");
removeSelectable(wallDownTag, true);
removeSelectable(wallLeftTag, true);
removeSelectable(wallUpTag, true);
removeSelectable(wallRightTag, true);
} else if (x == 45f){
Debug.Log("X = " + x);
Debug.Log("Remove wall!");
wallPartsDown(y);
wallPartsLeft(y);
wallPartsUp(y);
wallPartsRight(y);
}
}
void wallPartsDown(float y){
Debug.Log("Y = " + y);
if (y == 0f || y == 45f || y == 315f)
{
removeSelectable(wallDownTag, false);
if (y == 0f){
removeSelectable(wallLeftTag, true);
removeSelectable(wallUpTag, true);
removeSelectable(wallRightTag, true);
} else if (y == 45f){
removeSelectable(wallLeftTag, false);
removeSelectable(wallUpTag, true);
removeSelectable(wallRightTag, true);
} else if (y == 315f){
removeSelectable(wallLeftTag, true);
removeSelectable(wallUpTag, true);
removeSelectable(wallRightTag, false);
}
}
}
void wallPartsLeft(float y){
Debug.Log("Y = " + y);
if (y == 90f){
removeSelectable(wallDownTag, true);
removeSelectable(wallLeftTag, false);
removeSelectable(wallUpTag, true);
removeSelectable(wallRightTag, true);
}
}
void wallPartsUp(float y){
Debug.Log("Y = " + y);
if (y == 135f || y == 180f || y == 225f){
removeSelectable(wallUpTag, false);
if (y == 135f){
removeSelectable(wallLeftTag, false);
removeSelectable(wallDownTag, true);
removeSelectable(wallRightTag, true);
} else if (y == 180f){
removeSelectable(wallLeftTag, true);
removeSelectable(wallDownTag, true);
removeSelectable(wallRightTag, true);
} else if (y == 225f){
removeSelectable(wallLeftTag, true);
removeSelectable(wallDownTag, true);
removeSelectable(wallRightTag, false);
}
}
}
void wallPartsRight(float y){
Debug.Log("Y = " + y);
if (y == 270f){
removeSelectable(wallDownTag, true);
removeSelectable(wallLeftTag, true);
removeSelectable(wallUpTag, true);
removeSelectable(wallRightTag, false);
}
}
void removeSelectable(string tag, bool able){
removed = GameObject.FindGameObjectsWithTag(tag);
Debug.Log("Remove Selectable");
foreach (GameObject remove in removed){
remove.GetComponent<Renderer>().enabled = able;
}
}
void Update()
{
var camAngleX = cam.transform.eulerAngles.x;
var camAngleY = cam.transform.eulerAngles.y;
switchWall(camAngleX, camAngleY);
}
}
This code should set the rendering of the walls, based on the angle of the camera rotation (0, 45, 90, etc …), to false. When the room is perpendicular to the xz plane (X = 90 °) all the walls must be visible and so far all right. The problem comes when X = 45 °, because the condition x == 45f is always detected as false and does not enter the if except at the first frame, in which it enters and “eliminates” the wall.
I entered the Debug.Logs to see what functions it goes into and “Switch wall!” each frame is printed.
Could someone kindly give me some explanation as to why it doesn’t enter the if?