Godot Version
4.3
Question
I’m trying to make 2d platformer. There is an enemy that once the player jumps on it, it gets knocked down, kind of flies a little bit and when it lands it slides until is stopped by a wall.
While the enemy is in that knocked down state it can be eliminated by a projectile.
The weird thing here is that once is knocked down, while it hasn’t touched a wall yet, I can eliminate it with the projectile, but for some reason when it reaches a wall and it stops moving the projectile seems to try to move it, and the physics try to apply but it doesn’t trigger the queue_free() call.
Below is the code for my enemy and my projectile (Which I called blast)
Also, if it’s worth to note, the collisions seem to register as I tried printing the number of collisions and this number goes up when the blast reaches it
extends CharacterBody2D
class_name Enemy
@onready var sprite2D = $Sprite2D
const SPEED = 200.0
const JUMP_VELOCITY = -400.0
# Get the gravity from the project settings to be synced with RigidBody nodes.
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
var direction = 1;
@onready var isKnockedDown = false
func _ready():
sprite2D.flip_h = direction == 1
func _physics_process(delta):
if(isKnockedDown):
if not is_on_floor():
velocity.y += gravity * delta
var foundCollisions = move_and_slide()
if(foundCollisions):
for i in get_slide_collision_count():
var collision = get_slide_collision(i)
if (collision.get_collider() is Blast):
receiveDamage()
if(collision.get_collider().is_in_group("Wall")):
velocity.x = 0
return
if not is_on_floor():
velocity.y += gravity * delta
if(is_on_wall()):
direction *= -1
sprite2D.flip_h = direction == 1
velocity.x = SPEED * direction
var foundCollisions = move_and_slide()
func knockDown():
velocity = Vector2(100 * direction * -1, -500)
set_collision_mask_value(1, false);
set_collision_layer_value(3, false);
sprite2D.flip_v = true
isKnockedDown = true
func receiveDamage():
queue_free()
extends CharacterBody2D
class_name Blast
@export var direction:int
@export var speed:float
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
pass # Replace with function body.
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
pass
func _physics_process(delta: float) -> void:
velocity.x = speed * direction
var collision = move_and_collide(velocity * delta)
if collision and collisionWithWall(collision) and (collision.get_collider().is_in_group("Floor") or collision.get_collider().is_in_group("Wall")):
queue_free()
elif(collision and collision.get_collider() is Enemy and not collision.get_collider().isKnockedDown):
queue_free()
func collisionWithWall(collision: KinematicCollision2D) -> bool:
return collision.get_angle() > 1.20 and collision.get_angle() < 1.94