Hello, I'm a beginner in godot trying to make a fnaf game and I'm trying to set an x axis limit when the camera 2d moves across the screen according to the mouse position. But for some reason it only moves within the purple boarders and not across the full image, if anyone has an idea on how to fix this it won't fall on deaf ears, Thank you. The second image is the code.
#Trying to set camera2d x axis limit when moving across screen
1 messages · Page 1 of 1 (latest)
Heyo!
Hiya
Looking at the script, you have hardcoded -125 to 125. Is that your intent?
Honestly I'm not sure, I asked for help previously and was given this code so I'm just running with it. The full base image is 2200x1080. The games natural ... display window is 1920x1080.
The purple box shown in the first screenshot is your camera bounds
I feel like you are wanting to have the camera move and not show any empty space outside the bg image?
Correct.
Alright, gimme a few minutes
Alright.
extends Camera2D
@export_range(0.1, 10) var _cameraSpeed := 2.0
@export_range(1, 500) var _cameraPanDeadzone := 200
@export var _boundarySprite : Sprite2D
var _cameraBoundaries : Rect2
func _ready():
_cacheBoundaryInfo()
func _process(delta : float):
_processMovement(delta)
_keepInBoundary()
func _processMovement(delta : float):
var mousePos := get_global_mouse_position() - global_position
if mousePos.length() <= _cameraPanDeadzone:
return
global_position.x += mousePos.x * _cameraSpeed * delta
func _keepInBoundary():
if global_position.x < _cameraBoundaries.position.x:
global_position.x = _cameraBoundaries.position.x
elif global_position.x > _cameraBoundaries.end.x:
global_position.x = _cameraBoundaries.end.x
func _cacheBoundaryInfo():
if _boundarySprite == null:
push_error("Null sprite!")
return Rect2()
# Basing the camera boundary on the texture size and the sprite scale
var imageSize := _boundarySprite.texture.get_size() * _boundarySprite.scale
# Use the viewport rect to determine the camera size
# So the bounds of the camera do not leave the edge of the sprite
var viewportRect := get_viewport_rect()
var topLeft := (_boundarySprite.global_position - imageSize * 0.5) + Vector2(viewportRect.size.x * 0.5, 0)
var bottomRight := (_boundarySprite.global_position + imageSize * 0.5) - Vector2(viewportRect.size.x * 0.5, 0)
# Caching this for use above
_cameraBoundaries = Rect2(topLeft, bottomRight - topLeft)
So essentially, you drag and drop in the background image to use for the camera bounds. I exported some of those variables too and the camera movement will use delta time so it will appear to be smoother
Wow thank you! So I just replace this with my current code and it works?
Hopefully! I don't see any obvious dependencies on it
Maybe needs some changes if the origin or texture region were used, but give it a try first
An improvement to the script would be to have it automatically load the necessary BG image when you make multiple levels, but one step at a time
Well I input the code and it doesn't seem to be working, the cam won't rotate like before
The camera rotates?
Uhh, how do I say this. It panned according to the mouse. Like in the original fnaf game
also check the error logs
Any progress @steady elm ?
checking
Which values?
E 0:00:01:0219 Camera2D.gd:38 @ _cacheBoundaryInfo(): Null sprite!
<C++ Source> core/variant/variant_utility.cpp:1091 @ push_error()
<Stack Trace> Camera2D.gd:38 @ _cacheBoundaryInfo()
Camera2D.gd:13 @ _ready()
This is my error
So go to your inspector for the camera node
Should look like this, that error means you haven't dragged and dropped a ~~background ~~ boundary sprite in.