#Trying to set camera2d x axis limit when moving across screen

1 messages · Page 1 of 1 (latest)

steady elm
#

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.

steady elm
zenith lake
#

Looking at the script, you have hardcoded -125 to 125. Is that your intent?

steady elm
zenith lake
#

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?

zenith lake
steady elm
zenith lake
# steady elm 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

steady elm
zenith lake
#

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

steady elm
steady elm
zenith lake
#

It pans across you mean?

#

yeah, it was working for me, what value have you got set?

zenith lake
#

Any progress @steady elm ?

steady elm
steady elm
#

This is my error

zenith lake
#

So go to your inspector for the camera node

zenith lake