Changing KinematicBody2D's collision shape causes clipping

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By breeze4125

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)

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:

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.

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

Tato64 | 2021-05-27 03:41

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

breeze4125 | 2021-05-27 03:44

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!

maxwellc055 | 2021-05-27 03:44

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: Imgur: The magic of the Internet.

breeze4125 | 2021-05-27 04:37