[SOLVED] How do I create a magnet function?

Hello, as a result of a certain event in my game, I want the sphere to function as a magnet in a certain area and I want to attract the “coin” objects around it. There are so many coin named objects in the same scene below, I have no idea how to access them all at runtime.

I leave a sample code snippet below. How do I integrate this into my own project.

    const coinPosition = this.coin.getPosition();
    const playerPosition = this.entity.getPosition();
    coinPosition.lerp(coinPosition, playerPosition, 0.45);

        if(coinPosition.distance(playerPosition) < 5) {
             //coin is close enough, just pickup it

Game link: Ramp

“Coin” object structure

could you include a link to the overview or editor of the project?

1 Like

Hi @Onur_Ozturk,

This game plays nice already, it’s quite addictive! Well done.

For the magnet I imagine you will need to find coin entities that are close to the player up to a distance. In your game most likely you will have a place where you spawn the coins, there you can add them to an list, array so you can access them in your player script.

You can check the distance from the player and if it’s close enough, then start using the magnet code you drafted (you may have to clone / reuse a vector, to avoid using the one returned by getPosition()).


Editor Ramp

Hello, thank you for your good wishes. I have no idea how to do this. Is it possible for you to show a sample project?

Thanks again. :slight_smile: