#issue smoothly changing camera margin

30 messages · Page 1 of 1 (latest)

crisp ridge
#

basically I'm trying to make it so that the top margin of a Camera2D which is a child of a CharacterBody2d has a margin that changes depending on whether or not the character is on the floor. I wrote the following code:

if is_on_floor():
    $Camera2D.drag_top_margin = move_toward($Camera2D.drag_top_margin, 0, delta)
else:
    $Camera2D.drag_top_margin = 1

It works mostly, but there's a strange issue where there's a solid fraction of a second where the camera doesn't visibly move when the character touches the ground. I would like it to continuously move the second the player touches the ground. Weirder still, when I set it up so that the camera margin is printed to console, the value does consistently change but the change isn't visible. I've also tried lerp but still had same issue. (sorry if this is dumb or poorly written I'm new to godot)

tough kernel
#

Do you have a video showcasing the issue? I think it'd help

crisp ridge
#

one sec

#

it'll take a bit cuz I gotta like compress the vid and stuff

tough kernel
#

Take you time I need to be afk for 15 minutes

crisp ridge
#

cool

#

so you can see the character jumps on the higher ground and then it lags before the camera moves

#

damn it embed fail

tough kernel
#

That's because you're lerping the margin from the edge of the screen to the middle of it, but you character isn't at the edge of the screen, he's somewhere near the middle

#

So all the time it takes from the margin to reach your character is the time it's not moving

crisp ridge
#

hmmm ok

#

thanks

#

so wait do I gotta have it start from the position that the character is relatve to the screen?

tough kernel
#

yes, but it may be easier to just have a bit of code that moves the y of the camera towards the player whenever he's on the ground

#

And leave the drag margin to 1.0 or something

crisp ridge
#

ah ok thx

#

wait I cna't find in the docs how to set the camera's position

tough kernel
#

It inherits node2D so it's .global_position or something like that

crisp ridge
#

ok so I tried doing

if is_on_floor():
    $Camera2D.global_position.y = global_position.y
tough kernel
#

It teleports right?

#

You need a move_toward or a lerp to make it smooth

#

Something like
$Camera2D.global_position.y = move_toward($Camera2D.global_position.y, global_position.y, cameraspeed * delta)

crisp ridge
#

no it won't teleport it just won't move on the y axis

#

except for the movement from having a margin of 1

tough kernel
#

Weird

crisp ridge
#

I figured it would teleport but I would try that code before anything too crazy

tough kernel
#

Maybe the camera system interfers with it
You can try doing it with the margin again, but switching to zero as soon as the character touch the ground
And use position_smoothing_enabled and position_smoothing_speed to prevent teleport

#

I'm just reading the doc, never used it, so I'm just guessing, sorry if it doesn't work

crisp ridge
#

no no you're cool thank you for helping

#

ok.... so I've implemented your idea but now the camera drags behind the player as it moves along the x axis and it's kind of jittery