0
votes

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?

1
I think your solution is actually the easiest way to do this because you can tweak the collision shape later in the editor (if needed). However, I don't really understand the second part of your question. Why does the gravity change with the hitboxes? Have you thought about adding a gravity collision shape that stays the same but doesn`t collide with objects?magenulcus
Sorry for the late response. In my game, gravity can also be sideways. In sidewards gravity, the sliding hitbox would have to be rotated in order to still fit through one high gaps. This is only an example. If I rotated all hitboxes for all 4 gravitational directions, i would get the 12 hitboxes mentioned above.lbuchli
Also I dont really understand your suggestion. What benefits would a non-colliding collision shape have? How would you use it?lbuchli

1 Answers

1
votes

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.