Add a new script to the Coin root node.

The script will: 

Add variables:


spin_speed is how fast the coin will spin

bob_height is the distance the coin will move up and down

bob_speed is the rate a which the coin will go up and down

bob_timer will control our bob effect.

Capture Coin y pos as level loads:

Rotate the coin

Rotate the coin on the y-axis

This is done in the _process function.

The rotate function rotates along an axis by an amount.

In our case UP (Y-axis) by spin_speed

Drop a coin in the Level scene and test to see the effect

Bobbing the coin

We will use a sine wave to move the coin up and down.

Increase bob_timer by delta 

to measure the time that has elapsed.

Create a local variable called d 

to represent a point on our sin wave.

Essentially the sin function multiplies the magnitude of the wave by the frequency

If we update the global y position of the coin

It will now bob up and down

However,  a sine wave runs from -1 to 1

If we do not want the coin to go below the ground

We can add 1 and divide by two so that

the new wave runs from 0 to 1

Collecting the coin


Connect the body_entered signal so that we can detect 

when the player is overlapping the coin.

If the body is in the player group we add 1 to score

queue_free() destroys the coin object