+2 votes

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 fixedprocess 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!

in Engine by (16 points)

Please log in or register to answer this question.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.