I'm new to Godot and I've been trying to write some code where the camera limits change dynamically based on which area the character is in. (say for example the limits are confined to a room that is apart of a scene, and those limits change after a door opens allowing the camera to go forward past that room.)
However I've been having this issue where when I load into another scene and change the camera limits, instead of the camera screen being in the area I want it to be, it instead it locks onto this one corner, and the position of the camera itself changes to be far from the even when I don't explicitly edit it to be so in the code.
I've tried so many ways to fix it, such as:
- changing the camera zoom size so it fits between the limits when the limits change,
- changing the zoom size before it changes,
- setting the camera's position to the player's (which if anything made the situation worse as the camera's position just got even farther from where I wanted it to be),
- turning off and messing with crap like limit smoothing and the Camera's anchor node
- Reset Smoothing
- making it so the camera node is not a child of the characterbody2d node (I have both tried setting the camera2d's position to the characterbody and not setting it's position to the characterbody, and it's screen has still gone to the same spot twice)
I've also attempted to use clamping on the camera's position, but im not sure if i have been doing it right as I want it to be clamped to a specific area, but I don't know how to properly clamp it relative to that areas global position, I've tried doing it based off the values I got from the camera limit but even then it still gets stuck on that one spot when I do that anyways.
The only thing I've actually managed to get right was that the code that changes these limits is the main cause of this problem, (as deleting the code caused the problem to be resolved, and loading the next scene first instead of the other one and setting the camera's default limits to the ones it is set to in the code has the camera veiw set correctly and not stuck in a corner) but even then I don't know why it's been causing it and it has been frustrating me for hours on end. If any of you were to help me solve this problem I would be very grateful.
Here is the Code (Hope it is good enough to help):
`using Godot;
using System;
public partial class Camera2d : Camera2D
{
Area2D CameraChanger1;
Area2D CameraChanger2;
Vector2 GeneralPosition;
Node ParentBody;
CharacterBody2D PlayerBody;
CameraBussFile EventBus;
int left;
int right;
int top;
int bottom
// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
EventBus = GetNode<CameraBussFile>("/root/CameraBussFile");
GetNode<RoomBussFile>("/root/RoomBussFile").RoomChanged += Room2Camera;
EventBus.Connect("CameraNotific2", new Callable(this,"miniHallCamera"));
//I'm using all this complicated .Connect stuff instead of the standard "+=" connection method as for some reason my code wouldn't detect the custom signal from the eventbus unless I did it this way. I am pretty sure this isn't the problem though as the signal works just fine otherwise
PlayerBody = GetParent<CharacterBody2D>();
GeneralPosition = this.GlobalPosition;
}
// Called every frame. 'delta' is the elapsed time since the previous frame.
public override void _Process(double delta)
{
}
void Room2Camera()
{
if (GetNode<RoomBussFile>("/root/RoomBussFile").IsFromEnterance1 == true)
{
this.GlobalPosition = new Vector2(
Mathf.Clamp(GeneralPosition.X,left,right),
Mathf.Clamp(GeneralPosition.Y,top,bottom)
);
this.Zoom = new Vector2(40.0f, 40.0f);
CallDeferred("Debug");
left= this.LimitLeft ;
right = this.LimitRight ;
bottom= this.LimitBottom ;
top = this.LimitTop;
ResetSmoothing();
}
//The Scene that has these limits has 2 enterances, and I use a true/false marker to check which entrance it comes from to adjust the camera accordingly
else if (GetNode<RoomBussFile>("/root/RoomBussFile").IsFromEnterance1 == false)
{
miniHallCamera();
}
}
void miniHallCamera()
{
//this.LimitLeft = 275;
// this.LimitRight = 130;
// this.LimitBottom = 520;
// this.LimitTop = 250;
}
void Debug()
{
//Used this to find out what was happening with the position
GD.Print(this.GlobalPosition + "Camera");
this.Zoom = new Vector2(40.0f, 40.0f);
this.MakeCurrent();
this.LimitLeft = 450;
this.LimitRight = 460;
this.LimitBottom = 655;
this.LimitTop = 635;
}
}
`