lbuchli
lbuchli

Reputation: 21

Godot 3.0 Change collision box at runtime

In my (first) project in Godot, a simple platformer, I wanted to have the character slide. When the character slides, he should only be one tile tall so he can fit through smaller gaps. Because I didn't know any better solution, I changed the hitboxes like this:

(Also I used a seperate hitbox for walking)

enum Shape {
    NORMAL,
    WALK,
    SLIDE
}

func set_shape(name):

    $NormalCShape.disabled = true
    $WalkCShape.disabled = true
    $SlideCShape.disabled = true

    if name == Shape.NORMAL:
         $NormalCShape.disabled = false
    elif name == Shape.WALK: 

        $WalkCShape.disabled = false

    elif name == Shape.SLIDE:

        $SlideCShape.disabled = false

This isn't a very nice solution. Recently, my code got more complicated because I added different gravitational directions, which would result in 12 different hitboxes. I haven't found any better solution to do this, but I feel like there's got to be one.

So can anyone help me with this?

Upvotes: 0

Views: 2225

Answers (1)

Dylan Culfogienis
Dylan Culfogienis

Reputation: 33

Ultimately your solution will be the easiest solution as if you modify the geometry of the hitbox (which can be done by editing CollisionShape2D.shape.rect_extents assuming you're using a rectangular hitbox), you'll still have to store all 12 modified geometries by writing their RectExtents in code instead of modifying them in the editor.

An easier potential solution: Assuming your character uses KinematicBody2D, just change your code so that gravity applies a force to them in whichever direction is down, and then rotate the entire character. This way you only need 3 different CollisionShapes.

Upvotes: 1

Related Questions