0 votes

I'm trying to make a collectible that when the player touches it, it follows it until it disappears at a certain point.

I know how to make it go away at a certain point, but I have a problem for it to follow the player, I tried to use directionto but when it is at its destination it starts shaking so I tried to use Joints, but I couldn't find a way to set nodea and nodeb through code, except setdeferred, but it didn't work either.

There is a way to make it follow the player?, I am using a KinematicBody2D for the player and the candy object.

EDIT:
The answer works perfectly, but away from this, I decided to share some information about how Celeste Collectibles works in terms of claiming, just in case somebody it's doing something inspired by this amazing game and has been hours searching about how Celeste works.

Generally, to claim a Strawberry, the player must be in-ground, not at a specific point. Also, each tile has a property to check if the floor is valid for claiming the collectible or not (This is made for some tiles like bridges or moving platforms, but also can be applied to normal tiles, so not all the ground you can touch it's a place for claiming strawberries.) Hope this helps people who are curious about Celeste functions and are willing to do something similar to it.

Godot version 3.2.3
in Engine by (65 points)
edited by

1 Answer

+1 vote
Best answer

Never played Celeste so i dont know exactly how it works and looks, but anyways...

All of this will go in the script of the collectible, which will be a KinematicBody2D lets start with some vars:

var player = [method this node will use to find the player, for example get_node("/root/World/Player"), but you may choose other ways]

var playerpos = player.global_position

note: you could use a Position2D node in the player node and reference that point instead. This will allow you to make the collectible follow it further above or below, or even add some movement to that point like going up and down too add more "life" to the collectible.

Now, the code will be very simple:

if global_position.distance_to(playerpos) < [minimum distance]:
   move_and_slide(Vector2.ZERO)
else
   move_and_slide(playerpos - global_position).normalized() * [multiplier]

"[minimum distance]" will be a number, which as you may have guessed marks how close the collectible with be when it stops following.

"[multiplier]" will also be a number which changes the speed of the moving, so play around until you find one that works for you

Let me know how it goes!

by (307 points)
selected by

It works perfectly, Many thx!!

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected]odotengine.org with your username.