Changing hitbox for crouching


(Nate ) #1

Good evening FlashPunkians!

I have had this issue before and do not remember if I ever solved it… Basically I have a player, they have a normal hitbox and when they press down the hitbox changes sizes to effectively crouch through lower parts of the level.

The hitbox changes and all is good in that department. The problem is, when the player is crouching in a low ceiling part of the level, if they release the crouch key, the player returns to the normal hitbox state and gets clipped into the wall of the level, never to return… lol unless the player holds crouch again. I know this isn’t how a typical game would handle crouching.

Here is my crouching code:

//crouch to change hitbox
		if (Input.check(Key.S) || Input.released(Key.S) && collide("level", x , y))
		{
			setHitbox(16, 32, -10, -32);
		}
		else
		{
			//normal hitbox
			setHitbox(16, 48, -10, -16);
		}

Thanks guys!


(David Williams) #2

I did this by checking collision of the standing hitbox before alowing the player to stand, like this:

           if(this.Input.check(this.Key.DOWN))
            {
                if(!this.down)
                {
                    x = this.x - 5;
                    y = this.y + 10;
                    this.down = true;
                }
                this.grafic = new Image(this.gfxD);
                mask = new Hitbox(30, 10);
            }
            if(!(collide("level", this.x, this.y - 10)))
            {
                if(this.down)
                {
                    x = this.x + 5;
                    y = this.y - 10;
                    this.down = !this.down;
                }
                this.grafic = new Image(this.gfx);
                mask = new Hitbox(20, 20);
            }

(Nate ) #3

Nice I have no clue how I missed that! I simply added a nested if statement in my else to make sure the player isn’t colliding with the level before returning to the standing hitbox and it works great! Thanks Deamonr!


(John Andersson) #4

Sweet! I’m gonna use this as well. Didn’t even think about adding “crouch to access this part” in the game I’m working on. Awesome :slight_smile: