Pushing a box

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

Hi!

I’ve followed this tutorial:
http://docs.godotengine.org/en/latest/tutorials/2d/kinematic_character_2d.html

I’ve modified a bit the movement code for making the player push objects, but it doesn’t not work and I cant figure why…

This is the code:

func update_velocity():
	target_velocity = Vector2();
	
	if (Input.is_action_pressed("ui_left")): target_velocity.x -= 1;
	if (Input.is_action_pressed("ui_right")): target_velocity.x += 1;
	if (Input.is_action_pressed("ui_up")): target_velocity.y -= 1;
	if (Input.is_action_pressed("ui_down")): target_velocity.y += 1;
	
	if (target_velocity.length() > 1):
		target_velocity = target_velocity.normalized();
	
	if (Input.is_action_pressed("ui_run")):
		target_velocity *= running_speed;
	else:
		target_velocity *= walking_speed;

func process_movement(delta):
	velocity = velocity.linear_interpolate(target_velocity, .15);
	var motion = velocity * delta;
	
	motion = move(motion);
	
	if (is_colliding() && get_collider().is_in_group(constants.GROUP_PUSHABLE)):
		var pushed = get_collider().move(get_collision_normal().abs() * motion);
		motion = move(motion);
	
	if (is_colliding() && motion.length() > 0):
		var n = get_collision_normal();
		motion = n.slide(motion);
		velocity = n.slide(velocity);
		move(motion);

This both methods are called in the _fixed_process method.

The problem is in this part:

if (is_colliding() && get_collider().is_in_group(constants.GROUP_PUSHABLE)):
	var pushed = get_collider().move(get_collision_normal().abs() * motion);
	motion = move(motion);

The box (the collider object, a Kinematic2d body) seems to be moved (if I print “pushed” var it shows (0,0)), but I don’t know why the player is still colliding with it! It happens even if I do a queue_free() on the box…

Any advices?

Thanks in advance!