Hitbox Change and Movement Direction Issues

Godot Version

Godot 4.2.1

Question

Two main questions:

  1. How would I change the hitbox and visual elements of a player when a key is pressed? Right now I have a CharacterBody2D as my player with a ColorRect and a CollisionShape2D for visual and collision. When I press down, I want to make the player half the height. I also have a StaticBody2D node with a CollisionPolygon2D and a Polygon2D for the ground. I’m not quite sure how to change the level, but a similar thing could be used for the player.

  2. I have some code the moves the player, but when I hold left and up, the spacebar stops triggering the code. Any ideas?

Note: some of the code is nested, but it doesn’t show.

Heres the code:

extends CharacterBody2D

@export var walk_speed = 50
var screen_size# = get_viewport_rect().size
var camTarget = Vector2.ZERO
var canDash = true

signal playerLocation(camTarget)

func _ready():
velocity = Vector2.ZERO
screen_size = get_viewport_rect().size

func _process(delta):
camTarget = global_position
camTarget.x -= screen_size.x / 2
camTarget.y -= 200
_physics_process(delta)

func get_input():
if canDash:
if Input.is_action_just_pressed(“dash”):
canDash = false
if Input.is_action_pressed(“right”):
velocity.x += 1100
if Input.is_action_pressed(“slide”):
velocity.y += 400
if Input.is_action_pressed(“left”):
velocity.x -= 1100
if Input.is_action_pressed(“slide”):
velocity.y += 400

if Input.is_action_pressed("jump"):
	if is_on_floor():
		velocity.y = -200

if Input.is_action_pressed("slide"):
	velocity.x = velocity.x * 0.99
else:
	if Input.is_action_pressed("left"):
		velocity.x -= 0.75 * walk_speed
	if Input.is_action_pressed("right"):
		velocity.x += 0.75 * walk_speed
	velocity.x = velocity.x * 0.80
velocity.y = velocity.y + 4
if is_on_floor():
	canDash = true
	if abs(velocity.x) > 250:
		velocity.x = velocity.x * 0.65

func _physics_process(delta):
get_input()
move_and_slide()
playerLocation.emit(camTarget)

This could be a limitation of your keyboard. You should go here https://keyboardchecker.com/ to test and see if your keyboard input combination is recognized.

If it turns that combination doesn’t work… try other key combinations in different locations to see if there are other sections that allow for more then two keys to be pressed.

I think this should be possible,

you may need to synchronize the collision body change with the physics engine. If you change the shape size but it still collides I think that could be the issue. You will need to modify the shape resource like. $collionshape.shape.height = crouch_height

To modify the color rect is possible too. Since it inherited from Control there is probably some size parameters in there to modify.

I would recommend learning how to use sprite2d instead of using ColorRect. But for now it should be okay.

what do you mean by " the spacebar stops triggering the code" ?

Yeah, I checked last night and the spacebar, left, down, and up don’t work. I remapped it to z and x and it works. As for the size, that might work, although it might shrink from both sides, which wouldn’t work. I’ll look into a Sprite2D. Thanks!

P.S. Any idea on how to show code in the quote-like box?

It’s part of the markdown language you use three “```” back ticks. Which should be part of the tilde key, under the escape key, and you make a block like this

```
Her is some code
```

I put escape back slashes to keep markdown from changing it to a real code block.

Sorry for quotes, I just highlighted some else’s text and a little option pops up to quote text

I think it should work the same on a desktop browser as well.

Blockquote
there is also the block quote

Thanks! Would there be a way to load levels using some LoadScene() function or something similar?

Do you mean load/preload? With a .tscn file path, followed by instantiate()?

Var my_scene_node = load("res://path/to/my_scene.tscn").instantiate()

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.