Collision working intermittently and incompletely

Godot Version

4.2.2

Question

Hi team :slight_smile:

I’m doing something wrong with Collision - I’m trying to understand the most basic thing, and can’t seem to make it work

  • Have attached a CollisionShape2D to a CharacterBody2D with a nice square shape (a Player)
  • Have created a StaticBody2D, also with a nice square CollisionShape2D (a Wall)
  • Placed an instance of both in an otherwise empty Level node, at two different locations
  • Attached a simple “move a grid_sized jump with each keystroke, unless it would cause a collision, then do nothing instead” script to the Player

Expected behaviour - Player could move around freely, but couldn’t cross a Wall from any direction

Observed behaviour - Player is stopped by Wall on one side only. Can freely enter Wall collision shape from other three cardinal directions, and then gets stuck inside and cannot move in any direction

var direction = [Vector2(0, 1), Vector2(0, -1), Vector2(-1, 0), Vector2(1, 0)]
const GRIDSIZE = 128

func _physics_process(delta):
	if Input.is_action_just_pressed("Move Down"):
		move(direction[0])
#duplicated for each cardinal direction

func move(change):
	var amount = change * GRIDSIZE
	var collision = move_and_collide(position + amount, true)
	if collision:
		print("ow")
	else:
		position += amount
		print(position)

CollisionShapes are un-scaled, ie (1, 1)
Have checked that 'One way = false`

I’m obviously doing something wrong, and don’t understand this fundamental building block, any guidance greatly appreciated.

Let me know what other info I can provide to help, and big thanks in advance

dont remove move_and_collide code from _physics_process(delta)
this function needs to be called every frame inside _physics_process, regardless if you want to use the collision or not. right now it only access the move_and_collide if you press move down action

Oh! Fantastic - thank you, this is immensely helpful to know!

1 Like

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