How to keep distance between two controllable rigidbody2D?

Godot Version


Hello! So, i have a controllable Rigidbody2D (capsule shaped) that can move and balance. I need a second Rigidbody that will move with the first one, with the same logic, but i need them to keep constant distance between. The player rigidbody script:

extends RigidBody2D

const BALANCE_STRENGTH = 10000
const MOVE_SPEED = 200
var move_direction = Vector2.ZERO

const JUMP_FORCE = 1200

func _ready():

func _process(delta):
# Balancing logic

# Movement controls

func balance():
# Get current rotation and calculate tilt angle
var rotation = get_rotation()
var tilt_angle = rotation

# Apply torque to balance based on tilt angle
var balance_torque = -tilt_angle * BALANCE_STRENGTH

func handle_movement():
# Determine movement direction based on input
move_direction.x = 0
if Input.is_action_pressed(“move_right”):
move_direction.x += 1
if Input.is_action_pressed(“move_left”):
move_direction.x -= 1

# Normalize move direction if necessary
if move_direction.length_squared() > 0:
	move_direction = move_direction.normalized()

# Apply constant speed movement
var velocity = move_direction * MOVE_SPEED

Thank you for your attention!

Maybe use a var that reference the other rigid body and make sure that every frame if it moves, keep an track of the distance they were at before, and now, and add the difference to your position.