Hi! im doing a rogue-lite with procedural generation, the thing is when the player go to other room, the camera doesnt move to the other room and its because the value of currentRoom doesnt update, can you help me to know why?
Camera controller
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraController : MonoBehaviour
{
public static CameraController instance;
public Room currentRoom;
public int numeroDePrueba;
public float moveSpeedWhenRoomChange;
void Awake()
{
instance = this;
}
void Start()
{
}
// Update is called once per frame
void Update()
{
UpdatePosition();
}
void UpdatePosition()
{
if (currentRoom == null)
{
return;
}
Vector3 targetPos = currentRoom.GetRoomCenter();
transform.position = Vector3.MoveTowards(transform.position, targetPos, Time.deltaTime * moveSpeedWhenRoomChange);
}
Vector3 GetCameraTargetPosition()
{
if(currentRoom == null)
{
return Vector3.zero;
}
Vector3 targetPos = currentRoom.GetRoomCenter();
targetPos.z = transform.position.z;
return targetPos;
}
public bool IsSwitchingScene()
{
return transform.position.Equals(GetCameraTargetPosition()) == false;
}
}
RoomController
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class RoomInfo
{
public string name;
public int x;
public int y;
}
public class RoomController : MonoBehaviour
{
public static RoomController instance;
string currentWorldName = "Basement";
RoomInfo currentLoadRoomData;
Room currentRoom;
Queue<RoomInfo> loadRoomQueue = new Queue<RoomInfo>();
public List<Room> loadedRooms = new List<Room>();
bool isLoadingRoom = false;
void Awake()
{
instance = this;
}
private void Start()
{
LoadRoom("Start", 0, 0);
LoadRoom("Empty", 1, 0);
LoadRoom("Empty", -1, 0);
LoadRoom("Empty", 0, 1);
LoadRoom("Empty", 0, -1);
}
private void Update()
{
UpdateRoomQueue();
}
void UpdateRoomQueue()
{
if (isLoadingRoom)
{
return;
}
if (loadRoomQueue.Count == 0)
{
return;
}
currentLoadRoomData = loadRoomQueue.Dequeue();
isLoadingRoom = true;
StartCoroutine(LoadRoomRoutine(currentLoadRoomData));
}
public void LoadRoom(string name, int x, int y)
{
if (DoesRoomExist(x, y))
{
return;
}
RoomInfo newRoomData = new RoomInfo();
newRoomData.name = name;
newRoomData.x = x;
newRoomData.y = y;
loadRoomQueue.Enqueue(newRoomData);
}
IEnumerator LoadRoomRoutine(RoomInfo info)
{
string roomName = currentWorldName + info.name;
AsyncOperation loadRoom = SceneManager.LoadSceneAsync(roomName, LoadSceneMode.Additive);
while (loadRoom.isDone == false)
{
yield return null;
}
}
public void RegisterRoom(Room room)
{
room.transform.position = new Vector3(currentLoadRoomData.x * room.width,currentLoadRoomData.y * room.height,0);
room.x = currentLoadRoomData.x;
room.y = currentLoadRoomData.y;
room.name = currentWorldName + "-" + currentLoadRoomData.name + " " + room.x + ", " + room.y;
room.transform.parent = transform;
isLoadingRoom = false;
if(loadedRooms.Count == 0)
{
CameraController.instance.currentRoom = room;
}
loadedRooms.Add(room);
}
public bool DoesRoomExist(int x, int y)
{
return loadedRooms.Find(item => item.x == x && item.y == y) != null;
}
public void OnPlayerEnterRoom(Room room)
{
Debug.Log("Entered: " + room.name);
CameraController.instance.currentRoom = room;
currentRoom = room;
CameraController.instance.numeroDePrueba += 1;
}
}
Room
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Room : MonoBehaviour
{
public int width;
public int height;
public int x;
public int y;
// Start is called before the first frame update
void Start()
{
if(RoomController.instance == null)
{
Debug.Log("RoomController is null");
}
RoomController.instance.RegisterRoom(this);
}
private void OnDrawGizmos()
{
float gizmoOffset = 2f; // Ajusta este valor para controlar la distancia de desplazamiento
Gizmos.color = Color.red;
Gizmos.DrawWireCube(transform.position - new Vector3(0, gizmoOffset, 0), new Vector3(width, height, 0));
}
public Vector3 GetRoomCenter()
{
return new Vector3(x * width, y * height);
}
public Vector3 Vector3()
{
return new Vector3(x * width, y * height);
}
void OnTriggerEnter2D(Collider2D other)
{
if(other.tag == "Player")
{
Debug.Log("Player has entered the room");
RoomController.instance.OnPlayerEnterRoom(this);
}
}
}
Thanks for any help!