0 votes

Hello,

I have a CollisionShape2D attached to a KinematicBody2D. I want to change this collision shape when a certain state is entered. I looked around online, and it seems most people used an AnimationPlayer for this.

Here is my current setup:

func get_hitbox_from_state() -> void:
    match current_state:
        STATES.DJUMP:
            $collision_changer.play("djump_collision")
        _:
            if prev_state == STATES.DJUMP:
                $collision_changer.play_backwards("djump_collision")

And here is the AnimationPlayer setup (with snap 0.001s)
https://imgur.com/a/MOdevJx

All the AnimationPlayer does is tween from the default CollisionShape shape/position to the one I want when the state STATES.DJUMP is entered.

But now, when I try landing on something in game, I instantly clip through it. Here is a GIF of what happens:
https://imgur.com/a/v8HLIj5

I have tried having two collision shapes, one for default, one for STATES.DJUMP, and disabling one or the other depending on the state. I have also tried changing the CollisionShape's shape and position manually like so:

func get_hitbox_from_state() -> void:
    match current_state:
        STATES.DJUMP:
            $collision_box.shape.extents = Vector2(20, 20)
            $collision_box.position.y = -20
        _:
            $collision_box.shape.extents = Vector2(26.217, 66.251)
            $collision_box.position.y = 3.287

But nothing changes. Has anyone experienced this before or have a different solution for changing a CollisionShape on the fly?

I would be happy to answer any questions. Thank you in advance.

Godot version Godot v3.3-stable_win64
in Engine by (28 points)

Is there anything that may change the collision layer/mask somewhere else in the code?

Hi, thanks for the question. No, I have not touched the collision layers or masks in my project yet.

A couple thoughts, though I don't quite have an answer :/. Hopefully we can get to the bottom of this.
I know you said you've tried this, but I would stick with two different hitboxes for the two different states. I've noticed and heard of the CollisionShape system being finnicky when changing shape.
I see that the platform hitbox is quite thin, and the hitbox when jumping is of course smaller than when standing. Would you mind trying to make the roof platform thicker and letting us know the results? I've had clipping issues before with thin hitboxes.

At the very least we'll start eliminating potential issues!

Hello Maxwell,
Thank you for your reply and suggestions. I've changed it back to having two different hitboxes:

func get_hitbox_from_state() -> void:
    match current_state:
        STATES.DJUMP:
            $collision_box.disabled = true
            $djump_collision_box.disabled = false
        _:
            $collision_box.disabled = false
            $djump_collision_box.disabled = true

Because the roof collision box has one-way collision, I made its shape comically large for testing's sake. Unfortunately, it's still behaving the same way: https://imgur.com/a/Ex6RshW.

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 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 webmaster@godotengine.org with your username.