|
|
|
 |
Reply From: |
njamster |
I would do it the other way around and add an Area2D - I’m assuming you’re working in 2D here, but all things discussed here should apply to 3D as well - to the player! Then your player-script would need to contain something like this:
func _physics_process(delta):
if Input.is_action_just_pressed("ui_accept"):
for body in $Area2D.get_overlapping_bodies():
body.queue_free()
To avoid freeing other bodies on accident, consider adding your coconut-scene to a group and check if a body belongs to that group before freeing it:
func _physics_process(delta):
if Input.is_action_just_pressed("ui_accept"):
for body in $Area2D.get_overlapping_bodies():
if body.is_in_group("coconuts"):
body.queue_free()
So put the Area node on the Character, do I leave the Area node on the coconut aswell, as to have 2 area zones overlapping or should I just have the physics body on the coconut and my characters area reads when it’s in the zone of the coconuts physics body, I was looking towards _overlapping_bodies(): , I need to read more into them, Don’t fully understand their parameters. every reply is a big help thank you.
markelliot_94 | 2020-04-02 02:41
So put the Area node on the Character
Correct. It represents an area around the character where it will detect other bodies. These can be NPCs, enemy characters or - as in our case - items on the floor.
do I leave the Area node on the coconut aswell
For my code to work, you don’t have to. it check’s for overlapping bodies. However, you can also check for overlapping areas with get_overlapping_areas()
.
Calling these functions on an Area2D will just give you an array, filled with all bodies / others areas present in that Area2D at this moment in time. So the output will change depending on when you’re calling it. You can then loop over the entries and check out each one of them individually, like I do in my code with the groups.
njamster | 2020-04-02 09:52
Awsome It works great, that helps alot,I was way over complicating a simple thing I think one of my problems is coming in with no coding exp. at all expecting to get by with just online tutorials, I’ve started learning Python on the side to give me a better understanding of how everything works, I was going to use unity because they had more tutorials but couldn’t see my self not using Godot as I love it so much it’s just my lack of understanding. cheers for the feedback
markelliot_94 | 2020-04-03 02:49