Collision detection between Area2D and KinematicBody2D not updating continuously

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By disphing

Kinda a noob question :confused:

I’m currently working on a rhythm game, and it works by detecting when the buttons (Area2D) collide with the notes (KinematicBody2D), and getting the position of each note to determine the score. To get this to work I need the note’s position to be updating continuously, but I can’t seem to get that to work.

I’m using the _on_body_entered signal to get the position of each object that collides with the button, and although it does output something each frame, it seems to only be outputting the position from when it first detects the collision.

here’s the code for the button:

extends Area2D

signal button1press
signal button1release
var colliding_object = 'null'

func _on_Button1_body_entered(body):
colliding_object = body.global_position

func _process(delta):
if Input.is_action_just_pressed("button1"):
emit_signal('button1press')
if Input.is_action_just_released('button1'):
emit_signal('button1release')
print(colliding_object)

here’s the code for the note:

extends KinematicBody2D

export var note_speed = 5

func _process(delta):
move_and_collide(Vector2(0,100 * note_speed * delta))

here’s an example of the output i’m getting:

any help appreciated! sorry if this has been asked before

:bust_in_silhouette: Reply From: Inces

Collision is updated correctly on every body entered situation. You used high level script variable to hold information about global position of colliding object. So what happens is : note is colliding with button, colliding_object is set to position of this collision forever and in process() you are continously printing this set variable.

Although it is suppoesed to be rhytm game I don’t imagine how do You want it to be played. Can You say more details about it, or some screen from gameplay?
I thought it should be something like checking how far note is away from button when button is pressed. Yet You say You want to get notes position after it has collided with button ? If so then just do colliding_object = body, and in process print(colliding_object.global_position)

thanks for the response! i implemented the method you suggested and after a bit of tweaking to avoid errors, i got it working literally as i’m typing this - i didn’t expect the solution to be so simple but thanks for your help!

also sorry if i didn’t give enough info about the gameplay - it’s basically just a guitar hero clone. here’s a video of where i’m at so far (it’s not as laggy as it looks trust me). as with any rhythm game the score in the top right corner increases when you hit notes, and your accuracy determines that score.

thanks again!

disphing | 2021-11-15 12:11