Attempting to make box that the player can push slowly. Box has unintended interaction if it hits the player.

Godot Version

Godot 4.7

Question

Hello everyone, I am very new to godot and trying to learn by doing one thing at a time. Right now I’m trying to figure out how to make a box that moves slowly when the player pushes into it.

Here’s what it does right now:

https://youtu.be/vthA9ds_mLU

(had to put a link since im new)

Here’s the code in the player:
extends CharacterBody2D

@export var speed = 50
@export var engine = 1 #this very
var playerIdle = true

func _ready():
pass

func get_input():
#Get movement vector from input
var input_direction = Input.get_vector(“left”, “right”, “up”, “down”)
#Apply movement
velocity = input_direction * (speed*engine)

func idle():
#Controls when the player stops moving to reset speedup and set an idle variable
if velocity == Vector2.ZERO:
if engine > 1: engine-=.2
playerIdle = true

else:
	if engine < 3: engine+=.2
	playerIdle = false

func _animations():
#flip players sprite based on movement
if velocity.x > 0:
$PlayerSprites.flip_h = false
elif velocity.x < 0:
$PlayerSprites.flip_h = true
#walking/idle
if playerIdle: $PlayerSprites.play(“idle”)
else: $PlayerSprites.play(“walk”)

func _physics_process(_delta):
get_input()
idle()
_animations()
move_and_slide()

extends CharacterBody2D

@export var speed = 50
@export var engine = 1 #this very 
var playerIdle = true

func _ready():
	pass

func get_input():
	#Get movement vector from input
	var input_direction = Input.get_vector("left", "right", "up", "down")
	#Apply movement
	velocity = input_direction * (speed*engine)
	

func idle():
	#Controls when the player stops moving to reset speedup and set an idle variable
	if velocity == Vector2.ZERO:
		if engine > 1: engine-=.2
		playerIdle = true
		
	else:
		if engine < 3: engine+=.2
		playerIdle = false

func _animations():
	#flip players sprite based on movement
	if velocity.x > 0:
		$PlayerSprites.flip_h = false
	elif velocity.x < 0:
		$PlayerSprites.flip_h = true
	#walking/idle
	if playerIdle: $PlayerSprites.play("idle")
	else: $PlayerSprites.play("walk")

func _physics_process(_delta):
	get_input()
	idle()
	_animations()
	move_and_slide()
	
	for i in get_slide_collision_count():
		var col = get_slide_collision(i)
		if col.get_collider() is RigidBody2D:
			col.get_collider().apply_central_impulse(-col.get_normal() * 20)

Only some of your code is in the formatted blocks. Can you format the rest?

And it looks like the player code was pasted twice, or maybe you pasted two different versions of it. Did you mean to include the code for the box too?

If you want the box (a RigidBody2D) to simply accelerate at a slower pace, you can increase its mass.

If want the box to have a limited top speed, you have to make a script for the box that does that. For example:

extends RigidBody2D
# Script for your box

@export var max_speed = 5.0

func _integrate_forces(state: PhysicsDirectBodyState2D)
	var clamped_velocity = state.linear_velocity.limit_length(max_speed)
	state.linear_velocity = clamped_velocity

Alternatively, you can play around with the box’s linear_damp and linear_damp_mode to approximate the top speed you want for the player while still maintaining the same physically correct interaction with other objects.


I hope that helps. If not, please provide a detailed description of what you would like to achieve.