I am using move_and_slide() to move my player and enemy which are both CharacterBody2Ds. When using get_slide_collision_count() to check for collisions, sometimes a collision isn’t detected even though I visibly see the player and the enemy pushing each other around.
Try making sure you’re using _physics_process instead of _process.
If that doesn’t fix it, please share your code.
player code:
func _physics_process(delta: float) -> void:
if Input.is_action_pressed("W_pressed"):
velocity.y = -1
elif Input.is_action_pressed("S_pressed"):
velocity.y = 1
else:
velocity.y = 0
if Input.is_action_pressed("A_pressed"):
velocity.x = -1
elif Input.is_action_pressed("D_pressed"):
velocity.x = 1
else:
velocity.x = 0
velocity = velocity.normalized()*SPEED
move_and_slide()
for i in get_slide_collision_count():
print("player collided")
enemy code:
func _physics_process(delta: float) -> void:
move(delta)
move_and_slide()
for i in get_slide_collision_count():
print("enemy collided")
move function in enemy code just sets enemy velocity.
one thing I found out is that the enemy code actually detects collisions consistently, the problem is with the collision detection of the player which I am using to damage the player.
check your collision layer and collision mask.
if you want to see if your enemy is in collision with your character and vice versa you should do
for i in range(get_slide_collision_count()):
var collision = get_slide_collision(i).get_collider()
if collision.name == "Enemy":
print("do somthing")
ya both are on collision layer and mask 1. The thing is that the collision detection kinda works but is inconsistent.
also rn the only things I have colliding are the player and the enemy so I don’t need to specifically check if the collider is an enemy or a player.