Issue: 2D mid-point Camera pulling down one of 2 players. Help wanted!

Godot Version

v4.3.stable.official [77dcf97d8]

Sorry if I am missing something obvious, I have only been using Godot for just over a week or so. Thank you for any and all help you can offer.

Background

I have a simple 2-player top-down game with a camera that happily finds the mid point of both players by setting the position of camera as (player1.position + player2.position)/2.

Obviously I needed a way to stop the players running off the screen. So I surrounded the edges with 4 CollisionShapes2Ds under a StaticBody2D node - probably not the best way to do this, but I am still new, forgive me.

This works until either player runs down the screen, they are suddenly able to pull the whole camera down and pull the other player down with it, often through walls! The weird thing is it doesn’t appear to happen in any direction - one player can pull the other left and right. Neither player can pull the other player up the screen. Both players can use the camera to pull the other downwards. I wonder if it has something with the default gravity on Godot or something. But I am too new to be sure.

Question

So, my question is, is there a better way to stop both players running off the screen? Or some way to stop the camera moving the players with the setup I already have? If there is a good tutorial out there I haven’t found it yet, but I would appreciate any suggestions.

Any help welcome. Thank you in advance! Please feel free to patronise me with over explanation of better solutions and fixes. I’m still learning what Godot can do, so I will really appreciate it. Thank you for any help you can offer!

Can you show your player-code?
So this scene has a boundingbox which none of the players should be able to go through, but they still do? Can you also show your scenetree?

1 Like

Thank you for your reply!

No, they don’t go through the cameras bounding box. Players can move the other players character (which shouldn’t happen!) by moving down the screen, which in turn makes the cameras bounding box push the player down. The players can’t move through walls normally. But when they use the cameras bounding box they can pull the other player through everything.

Sorry if I didn’t explain that very well. I will see if I can get some footage of it happening, but hopefully it’s clear already.

I would just like it so if both player characters move too far away from each other (up to the edge of the cameras view/edge of the screen) they aren’t able to move any further. Neither player should be able to pull the other player anywhere, they should just meet an edge until one of the two players move with the other player again.

This is the code for the players script, it’s the same on both with a few name changes. But I think it’s more about the camera than the players.

extends CharacterBody2D
@onready var animated_sprite_2d = $AnimatedSprite2D

var speed = 55
var friction = 0.2
var acceleration = 0.1

func get_input():
var vertical = Input.get_axis(“player1_up”, “player1_down”)
var horizontal = Input.get_axis(“player1_left”, “player1_right”)
return Vector2(horizontal, vertical)

func animations(directions):
if directions.y != 0:
if directions.y < 0:
animated_sprite_2d.play(“walk_up_player1”)
elif directions.y > 0:
animated_sprite_2d.play(“walk_down_player1”)
if directions.x != 0:
if directions.x < 0:
animated_sprite_2d.set_flip_h(true)
animated_sprite_2d.play(“walk_side_player1”)
elif directions.x > 0:
animated_sprite_2d.play(“walk_side_player1”)
animated_sprite_2d.set_flip_h(false)
else:
animated_sprite_2d.set_flip_h(false)

func _physics_process(_delta):
var direction = get_input()
if direction.length() > 0:
velocity = velocity.lerp(direction.normalized() * speed, acceleration)
animations(direction)
else:
velocity = velocity.lerp(Vector2.ZERO, friction)
animated_sprite_2d.set_flip_h(false)
animated_sprite_2d.play(“idle_player1”)

move_and_slide()

https://youtu.be/K8NLRHxzdLY - here is a video of the issue happening, hopefully that makes it clearer as to what is happening. The player characters should just move up to the edge of the camera when they move in opposite directions. They shouldn’t be able to pull each other around with the cameras bounding box.

Okay yeah this is a little tricky.
Maybe you can check the distance and see if they are bigger than a certain threshhold(different threshold for x and y coordinate), if yes then they cant move further in this direction.

This might still have some problems though

Yeah I think you might be right. I have tried a few things, including things like this ( How to prevent player from moving outside the shape or area? - #2 by system ) and keeping the camera with any sort of boundary leads to the players getting pushed around at some point. So I am going to try (tomorrow, maybe, it’s too late here now!) removing the boundary from the camera and instead put a check on both players movement scripts with position.distance_to. If that becomes too large (like 100 pix or something) , then I should be able to do a position.direction_to to work out which directions to stop the players moving in. I think that might be the only way to stop this happening. I was kind of hoping there was something better built in already, like I had messed up on a tick box or something. Ah well.

Thanks for the help!

Ended up fixing it today.

https://youtu.be/Y6TBmqE2gE4 - have a look.

I didn’t have time to work on it this week. Now both player characters check how far the other one is and if they get too far away they then check which direction the players are from each other and the input controls for the opposite directions get turned off - they get turned on again as soon as one player moves a pixel closer. In addition to this, they activate a speech bubble with an array of lines to shout at each other!

I still think there must have been a setting that I needed to change to get the bounding box working correctly. But I think this feels a lot nicer, even if it is not yet perfect. I need to fix the animations so they don’t jump to idle when players are trying to run away from each other. But that’s not urgent. So it’ll stay like that for now.

Thank you so much for the help and the suggested fix!

1 Like