Collision detection and response

Godot Version: 4.2.1

Just started using godot and i’m not very experienced. I’ve been following this video so far

I’m having trouble getting a response with a mob collision shape and the player.
When the player enters the mobs “detection area” there is no response and I have no clue why.

Here is the code so far. In advance, thank you

extends CharacterBody2D

#gravity
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
var player 
var chase = false


func _physics_process(delta):
	# Add the gravity.
	if not is_on_floor():
		velocity.y += gravity * delta
		player = get_node("../../Player/Player")
		var direction = (player.position - self.global_position).normalized()
		if direction.x > 0:
			if chase == true:
				print("chase")
			else:
				print("nochase")
	move_and_slide()

func _on_playerdetection_body_entered(body):
	if body.name == "Player":
		chase = true



func _on_playerdetection_body_exited(body):
	if body.name == "Player":
		chase = false


Hi! Firstly, you can use three backticks (`) at the start and end when writing your code to show it in code format, making it easier to read, this helps people help you!

Secondly, have you copied and pasted everything? I can’t see anything after this bit -
if body.name == "Player":

That could be the reason why it’s not working! Another possibility is that the name you’ve given your Player node may be something different, a common mistake beginners make is mixing cases. Maybe you called your player node “player” with a lower case P?

If this doesn’t help, let me know!

Hi, thanks for the answer
Yeah, in the video the creator uses print() to check if it’s working and hasn’t continued, i suspect the problem is somewhere in the mathematics of the stuff

var direction = (player.position - self.global_position).normalized()

Thanks for letting me know how to show code, it should be updated now

So I’m guessing you’re not getting any chase vs nochase outputted so far? In that case, yes, it could be an issue. Are you getting any errors?

(sorry for not responding about names)
I have triple checked the names of the nodes that are called they should be alright.

yeah, when entering the enemys “detection area” i want it to print so I can check when I’m inside the area and when I’m exiting the area but I’m not getting a response

I’m not getting any error messages, so I’m a bit lost

Can you send a screenshot of your scene?

Can you also try changing the code in the if statement to this -

if direction.x > 0:
    print("right")
elif direction.x < 0:
    print("left")
else:
    print("nochase")

Should help us see where the error is. I have a few ideas but I want to narrow down first

After changing the code, I get some output (don’t think it’s correct)

My player is in the air and falls with the enemy until they hit the ground
While falling I get “left” outputted but it stops when hitting the ground but no output when entering or exiting the collision shape (circle)

Okay so reason why nothing was showing before would have been that you were only checking if your player was to the right of the frog, nothing else.

Let’s change the if statement again to this :

if direction.x != 0:
    if chase == true:
        print("chase")
    else:
        print("nochase")

Oh and can you also post a screenshot of the frog scene?

It now prints “nochase” until the player hits the ground but still won’t give an output when entering or exiting the collision shape

GOT IT! Or at least, part of it.

You haven’t connected your Playerdetection node to your frog script. You can see the youtuber do this at 55:11 in your video, you’ll know you’ve done it properly when you see the little signal icon to the right of the PlayerDetection node.

Fingers crossed that solves all the issues!

It currently dosen’t do anything but (i think) that might be because we messed with the code. should I revert back to the youtubers code or?

(the node should be connected with the others now)

Wait, nevermind it works (forgot to add print “chase” to check if it worked)

1 Like

Thank you so much for the help :slight_smile: i really don’t think I would have a caught that before way too much time had passed if at all

1 Like

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