How do i stop characters leaving the edge of the screen

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By BFGames
:warning: Old Version Published before Godot 3 was released.

Hi,

I’ve been playing about with Godot for about a month now, doing tutorials and testing out a few things, and have started my own mini project, but I’ve run into a bit of a problem.

Basically, I have two kinematic body types simultaneously controlled by the same inputs, and a camera that follows the average of their x positions, which works fine … to a point. However, I want to implement obstacles into the game and if one gets caught on an obstacle and the other cam continue, both of them will be out of shot.

I have a makeshift where I’ve added a third invisible kinematic body that has the average position between the two and have the camera following that, and I call a function on the first two kinematic bodies if they get too far away from the centre and they have a raycast colliding (i.e. they’re trapped because the other has gone too far).

Far from an elegant solution and one that is bound to cause problems down the line - I’ve toyed about with raycasts and viewports to create alternative approaches, but i’m at a bit of a loss now.

TL;DR: Is there a way that I can stop two simultaneously-controlled objects that share the same camera from getting too far apart and disappearing out of shot?

Thanks in advance!

:bust_in_silhouette: Reply From: quijipixel

Just calculate their distance with their current position: (pos2 - pos1).length(). If the length is bigger than a constant value that represents the maximum distance for the camera, then just block your movement routines. Other thing that you could try is zooming the camera out. If the distance is greater than what the camera can show, then zoom out the camera as much as needed.

Thanks for your help - I did start with something similar and calculated it as an abs() but I think the trouble I had with it came when I was trying to add limiting functions when one was in danger of going out of the screen.

I’ll try again and see how I get on - think I’ve been second-guessing myself too much!

BFGames | 2017-09-10 15:30