All Questions

Community

Light
posted to: Bumpers (solution)

Enemy won't die or detect player

I've been stuck on this for far too long now...

It worked before I made the bumper, but now something changed and the Enemy doesn't die. I put a breakpoint in the signal for debugging, but aside from being triggered at the beginning of the level, it never fires again. The player is still able to bounce on the Enemy though

This is my code for the Enemy:

extends "res://Src/Actors/Actor.gd"

func _ready() -> void:
	_velocity.x = -Speed.x
	set_physics_process(false)

func _on_StompDetector_body_entered(body: Node) -> void:
	var physics_body := body as PhysicsBody2D

	if physics_body.global_position.y > get_node("StompDetector").global_position.y:
		return
	get_node("EnemyCollisionRect").disabled = true
	queue_free()

func _physics_process(delta: float) -> void:
	_velocity.y += Gravity * delta
	if is_on_wall():
		_velocity.x *= -1.0
	_velocity.y = move_and_slide(_velocity, FLOOR_NORMAL).y

After literally 2 hours of trying to figure this out, I randomly got it to kill the Enemy... I still have no idea why the Signal is being triggered at the start of the level, and why it is basically impossible to kill this Enemy... Is this related to a similar question posted about Enemies not dying?

Example: https://gfycat.com/SlipperyLavishGreatargus

  • Nathan Lovato replied

    It's most likely the positions of your collisions shapes and area nodes that are causing trouble. It's because of these lines of code:

    	if physics_body.global_position.y > get_node("StompDetector").global_position.y:
    		return

    We're comparing the position of the player with that of the StompDetector node. And so if the StompDetector happens to have its collision shape placed lower than it on the vertical axis, the condition will almost always be true and the enemy will rarely die when being stomp.

    This bit is a little finicky.

    If you keep having trouble with that, could you upload your Godot project as a zip archive so I can directly look at the scenes in Godot? You can upload it on a cloud folder like dropbox or drive.

  • Light replied

    Thanks for getting back to me!

    The reason I'm confused by your explanation is this: If it is indeed that line of code, wouldn't that require the code before it too also execute? 

    Given this code:

    func _on_StompDetector_body_entered(body: Node) -> void:
    	var physics_body := body as PhysicsBody2D
            print("I was printed")
    	if physics_body.global_position.y > get_node("StompDetector").global_position.y:
    		return
    	get_node("EnemyCollisionRect").disabled = true
    	queue_free

    If the issue was really if physics_body.global_position.y > get_node("StompDetector").global_position.y:Wouldn't that require the print above it to fire(it doesn't). I'm under the impression that it never actually calls _on_StompDetector_body_entered. Either the areas aren't making contact, or something else is causing this.


    This isn't a critical issue cause I'm just trying to learn the basics, but knowing why this is happening will help me learn and prevent similar issues in the future. So if you want the code, I'll upload it, but don't feel like I need this solved to be happy with the course. It has been amazing!

  • Nathan Lovato replied

    If the function rarely runs, then yes it's most likely that due to their position compared to the colliders that make the character bounce, the areas rarely get to overlap.

  • Light replied

    So that's what I'm struggling with... How should I position them?

    This is after I refactored it btw

  • Nathan Lovato replied

    Could you upload your project? It's difficult to help in that case without debugging directly in Godot.

  • Light replied

    Here you go! 
    https://drive.google.com/file/d/151nIV59AaTVSeyqg0JgigmFGYdgVtP73/view?usp=sharing

    It is a lot better than how it was in the video I showed (after moving the collision box for the bumper) but it is still fairly unreliable :(

  • Nathan Lovato replied

    So the player was already bumping, meaning on the player's side things were okay.

    It's on the enemy that i found you needed to move the StompDetector node up and its StopZone a bit up as well. This setup works well for me:

    Note that using an area like that isn't perfect so there'll always be some cases where, without a more detailed collision shape and perhaps a little more code, the detection won't work perfectly (I didn't go further down that route because this is a beginner course). But with something like the image above, you should get much more satisfying results.

  • Light replied

    Thank you so much for taking a look at this for me!

    I'll try to build a few small prototypes in hopes of finding the more optimal solution for this problem. For now, I'll keep using this method with your tweaks. 

    Thanks again Nathan, I owe you!

  • Nathan Lovato replied

    You're welcome.

    We added a bonus lesson with an alternative way to code the stomp. It's all code but pretty reliable. This could solve your problems: https://gdquest.mavenseed.com/lessons/a-simpler-way-to-stomp

  • Light replied

    That is perfect <3

    Thank you so much!