Add a new script to the Coin root node.
Add a new script to the Coin root node.
The script will:
The script will:
- Allow the coin to be collected.
- Make the coin bob up and down
- Rotate the coin continuously.
- Remove the ready function
Add variables:
Add variables:
spin_speed is how fast the coin will spin
spin_speed is how fast the coin will spin
bob_height is the distance the coin will move up and down
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_speed is the rate a which the coin will go up and down
bob_timer will control our bob effect.
bob_timer will control our bob effect.
Capture Coin y pos as level loads:
Capture Coin y pos as level loads:
Rotate the coin
Rotate the coin
Rotate the coin on the y-axis
Rotate the coin on the y-axis
This is done in the _process function.
This is done in the _process function.
The rotate function rotates along an axis by an amount.
The rotate function rotates along an axis by an amount.
In our case UP (Y-axis) by spin_speed
In our case UP (Y-axis) by spin_speed
Drop a coin in the Level scene and test to see the effect
Drop a coin in the Level scene and test to see the effect
Bobbing the coin
Bobbing the coin
We will use a sine wave to move the coin up and down.
We will use a sine wave to move the coin up and down.
Increase bob_timer by delta
Increase bob_timer by delta
to measure the time that has elapsed.
to measure the time that has elapsed.
Create a local variable called d
Create a local variable called d
to represent a point on our sin wave.
to represent a point on our sin wave.
Essentially the sin function multiplies the magnitude of the wave by the frequency
Essentially the sin function multiplies the magnitude of the wave by the frequency
If we update the global y position of the coin
If we update the global y position of the coin
It will now bob up and down
It will now bob up and down
However, a sine wave runs from -1 to 1
However, a sine wave runs from -1 to 1
If we do not want the coin to go below the ground
If we do not want the coin to go below the ground
We can add 1 and divide by two so that
We can add 1 and divide by two so that
the new wave runs from 0 to 1
the new wave runs from 0 to 1
Collecting the coin
Collecting the coin
Connect the body_entered signal so that we can detect
Connect the body_entered signal so that we can detect
when the player is overlapping the coin.
when the player is overlapping the coin.
If the body is in the player group we add 1 to score
If the body is in the player group we add 1 to score
queue_free() destroys the coin object
queue_free() destroys the coin object