Top Down Soccer Ball "Kick" Action

somersetjc

• Posts: 40
So I'm making a top down soccer game and have made a player character and a ball. I want the player to be able to press a button to be able to kick the ball and apply force to it and not just push it around. How would I go about doing this?

JeffreyDriver

• Posts: 2262
There's several things to take into account.

1. You need to know if the ball is close enough to be kicked
2. Is it in a position to be kicked? E.g not behind the player
3. Which way is the ball to travel?
4. What variations are there? Speed? Curve etc.?

There's different ways of doing this:
The simplest way would be to have an invisible actor that sits in front of the player and moves as the player moves. This actor doesn't need a collision shape.
Then check if the ball is within a certain distance of the invisible actor.
If it is, when the player kicks the ball, get the current direction of the player and use that to send the ball in the right direction.

You can check the distance using Pythagorean theorem: If sqrt (( x of obj1 - x of obj2 )^2 + ( y of obj1 - y of obj2 )^2 ) < distance
There's also the Easy Math extension with a block to calculate distance.

As for number 4, that's more complex. Do you need any variations?

somersetjc

• Posts: 40
There's several things to take into account.

1. You need to know if the ball is close enough to be kicked
2. Is it in a position to be kicked? E.g not behind the player
3. Which way is the ball to travel?
4. What variations are there? Speed? Curve etc.?

There's different ways of doing this:
The simplest way would be to have an invisible actor that sits in front of the player and moves as the player moves. This actor doesn't need a collision shape.
Then check if the ball is within a certain distance of the invisible actor.
If it is, when the player kicks the ball, get the current direction of the player and use that to send the ball in the right direction.

You can check the distance using Pythagorean theorem: If sqrt (( x of obj1 - x of obj2 )^2 + ( y of obj1 - y of obj2 )^2 ) < distance
There's also the Easy Math extension with a block to calculate distance.

As for number 4, that's more complex. Do you need any variations?

Thanks a lot for your reply.  I don't need there to be a curve put on the ball but I would like to have a mechanic in which a meter indicates how hard you kicked the ball and thus affects the speed of the ball.

JeffreyDriver

• Posts: 2262
Thanks a lot for your reply.  I don't need there to be a curve put on the ball but I would like to have a mechanic in which a meter indicates how hard you kicked the ball and thus affects the speed of the ball.

Is this for mobile or desktop/flash?

One method would be to calculate it on the length of time the button is held down. Use a number attribute that increments as the button is held down then when it's released use that value to calculate the speed.

somersetjc

• Posts: 40
Thanks a lot for your reply.  I don't need there to be a curve put on the ball but I would like to have a mechanic in which a meter indicates how hard you kicked the ball and thus affects the speed of the ball.

Is this for mobile or desktop/flash?

One method would be to calculate it on the length of time the button is held down. Use a number attribute that increments as the button is held down then when it's released use that value to calculate the speed.

It's on desktop/flash. I'll try all this out thank you.