0

So I was tasked with making a 2d video game for a final project for my game dev class this semester. I have been following along to a binding of Isaac like tutorial on youtube and I'm worried that outdated methods might have screwed me over. After adding a dungeon crawler I can't seem to get my camera to stay in the start room from the beginning, it teleports to a random room until I cross over a collider then it will come back to the player. Any help would be greatly appreciated!! Here's the code for the camera: `

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraController : MonoBehaviour
{

    public static CameraController instance;
    public Room currRoom;
    public float moveSpeedWhenRoomChange;

    void Awake()
    {
        instance = this;
    }

    // Update is called once per frame
    void Update()
    {
        UpdatePosition();
    }

    void UpdatePosition()
    {
        if(currRoom == null)
        {
            return;
        }

        Vector3 targetPos = GetCameraTargetPosition();

        transform.position = Vector3.MoveTowards(transform.position, targetPos, Time.deltaTime * moveSpeedWhenRoomChange);
    }

    Vector3 GetCameraTargetPosition()
    {
        if(currRoom == null)
        {
            return Vector3.zero;
        }

        Vector3 targetPos = currRoom.GetRoomCentre();
        targetPos.z = transform.position.z;

        return targetPos;
    }

    public bool IsSwitchingScene()
    {
        return transform.position.Equals( GetCameraTargetPosition()) == false;
    }
}

I was expecting the camera to stay inside of the start room until the character was moved into a different room. I tried adding a cooldown to the function that moved the camera figuring maybe it was just bugged from randomly generating different rooms but to no avail. I am very new to gamedev and c# so I will take any advice I can get!

James B
  • 1
  • 1
  • Is your currRoom variable in the CameraController script is assigned to the starting room when the game begins? – Abdulmajeed Apr 07 '23 at 09:53

0 Answers0