Collisions with KinematicBody2D moving along a PathToFollow2D
Asked Answered
P

2

0

Hello,
I am a newbie in godot, which I find fantastic !
I have succeed moving a sprite along a PathToFollow2D.
So, hierarchy of the node is: Path2D->PathToFollow2D->Sprite
I move the sprite from script of Path2D, and emit a signal when sprite has gone at the end:

func _process(delta):
	if _run:
		$PathFollow2D.set_offset($PathFollow2D.get_offset() + 200 * delta)	
	if $PathFollow2D.get_unit_offset() == 1.0:
		_run = false
		emit_signal("finished", _id, _currentPath)

I can launch as many of this node as I want, and set same curve to Path2D, so all sprite go the same way. Now, I would like to add collision detection, so all sprite line up at the end of the way. I have seen explanation about KinematicBody2D using move_and_collide, but none about following a path. Is KinematicBody not the right type ?
I could not find any example of this, so, if someone could give me an hint, that would be appreciated.
Thanks,
Claude

Preiser answered 3/3, 2023 at 17:7 Comment(0)
P
0

FYI, I chose another manner to solve my issue, since it appear to me that I have not enough possibility with the way KinematicBody2D handle collisions.
I put a Area2D as a child of my sprite.
Area2D launch events when another area enter and exit itself.
This way, I am able to set my _run variable to false, which stops the PathTofollow increasing, and then finally the sprite.

Preiser answered 11/3, 2023 at 13:20 Comment(0)
G
0

I just found the PathFollow node myself and it looks really useful from what I've read if it's applying direct transforms you've already figured out the right way to deal with it as the kinematic body won't collide with anything when used in this manner as you observed it will just clip through things. Another thing you can do is you can use raycasts to sort of fake collision and have a bunch of raycasts detect if the node is about to collide with something and push it away from the collision in question. If you want to have actual physics, then you'll need to re-write the code to use move_and_slide or move_and_collide or you'd need to use a rigidbody.

Something worth noting, I saw you've put the Godot 3 tag on your topic, Godot 4 has a lot of very useful built in navmesh agent stuff for 2D if you're looking at various automated movement for your enemies and things like that, it would be worth exploring.

Ginsburg answered 11/3, 2023 at 13:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.