Create a new Scene
Create a new Scene
Add an Area2D node
Add an Area2D node
Call it Coin
Call it Coin
Save the Scene as Coin
Save the Scene as Coin
Add a Sprite as a child
Add a Sprite as a child
Use tile_0151.png. as the texture.
Use tile_0151.png. as the texture.
Add a CollisionShape2D
Add a CollisionShape2D
Add a Circle Shape
Add a Circle Shape
Surround the coin
Surround the coin
Script the coin to move
Script the coin to move
Add a script to the Coin node
Add a script to the Coin node
Add the variables
Add the variables
Note that we can only set the coins starting y position once the node is ready
Note that we can only set the coins starting y position once the node is ready
bob_height determines how many pixels the coin will move up and down by.
bob_height determines how many pixels the coin will move up and down by.
bob_speed is how fast the coin moves up and down
bob_speed is how fast the coin moves up and down
start_y records the y position the coin needs to move back to when bobbing
start_y records the y position the coin needs to move back to when bobbing
t is a time value and will increase with every frame
t is a time value and will increase with every frame
We will move our coin up and down using a sin wave
We will move our coin up and down using a sin wave
This is just a repeating pattern over time.
This is just a repeating pattern over time.
We will increase t and then pass that to a built in sin wave function
We will increase t and then pass that to a built in sin wave function
We will get a value between 0 and 1
We will get a value between 0 and 1
We will save this in a variable called d (sin delta / change in sin over time)
We will save this in a variable called d (sin delta / change in sin over time)
This formula moves along a sin wave at a rate of t * bob_speed
This formula moves along a sin wave at a rate of t * bob_speed
Then we add 1 and divide by 2
Then we add 1 and divide by 2
To change the output from -1 and 1 to
To change the output from -1 and 1 to
0 and 1
0 and 1
Now we can add a line to move the coin up and down between 0 and 1
Now we can add a line to move the coin up and down between 0 and 1
To collect the coin we must
To collect the coin we must
add the body_entered signal and connect it.
add the body_entered signal and connect it.
Then code the signal in the coin script to
Then code the signal in the coin script to
increase the score and destroy the coin.
increase the score and destroy the coin.
Now you can add coins to the level for the player to collect.
Now you can add coins to the level for the player to collect.