The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates.

godotengine.org | Twitter

0 votes

So I'm trying to have the camera get pulled in the direction of the mouse, but stop at the border of the screen. It works, somewhat, but in the wrong direction. Adjusting the camera scale doesn't seem to affect it. Some help would be very much appreciated!

Video Example: https://streamable.com/zfqeur
Notice how the camera moves towards the sides. It moves down as well, but only when it's towards the bottom-right.

Camera script (should be unrelated but just in case):
extends Camera2D

This script assumes that the zoom.x and zoom.y are always the same.

var currentzoom
var min
zoom
var maxzoom
var zoom
factor = 0.75 # < 1 = zoomin; > 1 = zoomout
var transition_time = 0.25

func ready():
max
zoom = zoom.x
minzoom = maxzoom * zoom_factor

func zoomin(newoffset):
transitioncamera(Vector2(minzoom, minzoom), newoffset)

func zoomout(newoffset):
transitioncamera(Vector2(maxzoom, maxzoom), newoffset)

func transitioncamera(newzoom, newoffset):
if new
zoom != currentzoom:
current
zoom = newzoom
$Tween.interpolate
property(self, "zoom", getzoom(), currentzoom, transitiontime, Tween.TRANSLINEAR, Tween.EASEINOUT)
$Tween.interpolateproperty(self, "offset", getoffset(), newoffset, transitiontime, Tween.TRANSLINEAR, Tween.EASEIN_OUT)
$Tween.start()

Player Script:

The main code I'm using to move the camera:
func process(delta):
mouse
offset = (getviewport().getmouseposition() - getviewport().size / 2)
camera.position = lerp(Vector2(), mouseoffset.normalized() * 250,
mouse
offset.length() / 1000)
Code for zooming in camera (just in case):
if Input.isactionpressed("zoomin"):
$Camera2D.zoomin(position - $Camera2D.getcameraposition())
if Input.is
actionpressed("zoomout"):
$Camera2D.zoom
out(Vector2(0, 0))

in Engine by (105 points)

Im just putting the script here, in future use the {} to show script

var currentzoom
var minzoom
var maxzoom
var zoomfactor = 0.75 # < 1 = zoomin; > 1 = zoomout
var transition_time = 0.25

func ready():
    maxzoom = zoom.x
    minzoom = maxzoom * zoom_factor

func zoomin(newoffset):
    transitioncamera(Vector2(minzoom, minzoom), newoffset)

func zoomout(newoffset):
    transitioncamera(Vector2(maxzoom, maxzoom), newoffset)

func transitioncamera(newzoom, newoffset):
    if newzoom != currentzoom:
    currentzoom = newzoom
    $Tween.interpolateproperty(self, "zoom", getzoom(), currentzoom, transitiontime, 
    Tween.TRANSLINEAR, Tween.EASEINOUT)
    $Tween.interpolateproperty(self, "offset", getoffset(), newoffset, transitiontime, 
    Tween.TRANSLINEAR, Tween.EASEIN_OUT)
    $Tween.start()

Player Script:

#The main code I'm using to move the camera:
func process(delta):
    mouseoffset = (getviewport().getmouseposition() - getviewport().size/2)
    camera.position = lerp(Vector2(), mouseoffset.normalized() * 250,
    mouseoffset.length() / 1000)

    if Input.isactionpressed("zoomin"):
        $Camera2D.zoomin(position - $Camera2D.getcameraposition())
        if Input.isactionpressed("zoomout"):
        $Camera2D.zoomout(Vector2(0, 0))

Oh, thanks. I'm new to this forum, or posting on forums in general :/

did it work?

1 Answer

0 votes

You can actually set a maximum length the camera travels for. You find this in the "limit" section of the camera2D node. What it does is it makes the camera stop following the player after a certain point on either the x or the y axis has been passed. Try this and just fiddle around with it and you'll get it.

You can edit this effect through script by using the [example] code:

$Camera2D.limit_left = -200
$Camera2D.limit_right = 200
$Camera2D.limit_bottom = 200
$Camera2D.limit_top = -200
$Camera2D.limit_smoothed = -200

Note that left won't work unless it is a negative, same with top, and vice versa for bottom and right. I have not yet tested the smoothed method.

by (309 points)
edited by
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.