Discuss Scratch

Groundzerostudios
Scratcher
10 posts

Need help with a moving script.

Recently i have added more code to my tank game, however i cannot figure out a solution for the moving script for the player and enemies.
You see, the enemies run on a script that will make them move towards the player a random about of steps depending on the type of tank. However they will always point towards the player while doing this, so i wanted to know if there was some way i could incorperate a ¨AI¨ that will have the enemies move up/down/left/right/diagonal towards the player, basically path finding.
As for the player the script allows you to move diagonally but its not smooth, is there a way to make the tank go diagonal depending on the keys pressed? (And face diagonally as well)
For example if W and D are pressed the tank will move diagonally to the right. (And face that direction as well!)
when green flag clicked
forever
if <key [ w] pressed?> then
point in direction ( 0)
move (2) steps
end
This is the script for moving the tank, repeated 4 times for WASD and adjusted to face the direction and move that way.
Edit : I actually found a way to make the tank move diagonally by using this script
when green flag clicked
forever
if <<key [ w] pressed?> and <key [ d] pressed?>> then
point in direction ( 45)
move (3) steps
end
end

Last edited by Groundzerostudios (Yesterday 04:51:08)

Jareddddddd
Scratcher
1000+ posts

Need help with a moving script.

This is a very good question, and a bit more complicated than it seems! Lets take it one by one.
1 - Enemy AI
For an enemy's ai to move to something that is not the player, you have to ask where is it going to move to? Lets say that we have 6 variables - player X/Y, the target X/Y, and for the example, the player's X/Y velolcity.
Lets take Pinky from Pac-Man for example. It's AI always wants to move 4 tiles ahead of the player's movement. From this, we can set the target to the player's position, plus the direction of the player's movement.

set [target X v] to ((player X) + ((player X velocity) * (3)))
set [target Y v] to ((player Y) + ((player Y velocity) * (3)))

Once we have where the enemy wants to go, we need to actually implement how it will get there.
For this, we will need a script that allows us to point at a set of coordinates, and move towards it.
Point towards (X, Y)

Mathematical form:
(([atan v] of ((x) / (y))) + ((180) * <(y) < [0]>))

Custom block form:
define point towards (x) (y) 
point in direction (([atan v] of (((x) - (x position)) / (((y) - (y position)) + (0)))) + ((180) * <(y) < (y position)>))


How this works:
The ‘atan’ operation takes the ratio of Y to X and produces the angle it would be at. Adding the (180 * condition) accounts for negative directions, so it would work for the full 360 degrees


Now, I will leave the rest to you to put the pieces together and find out how to make your AI.
=======================
2 - Diagonal Movement

editing later
Groundzerostudios
Scratcher
10 posts

Need help with a moving script.

Jareddddddd wrote:

This is a very good question, and a bit more complicated than it seems! Lets take it one by one.
1 - Enemy AI
For an enemy's ai to move to something that is not the player, you have to ask where is it going to move to? Lets say that we have 6 variables - player X/Y, the target X/Y, and for the example, the player's X/Y velolcity.
Lets take Pinky from Pac-Man for example. It's AI always wants to move 4 tiles ahead of the player's movement. From this, we can set the target to the player's position, plus the direction of the player's movement.

set [target X v] to ((player X) + ((player X velocity) * (3)))
set [target Y v] to ((player Y) + ((player Y velocity) * (3)))

Once we have where the enemy wants to go, we need to actually implement how it will get there.
For this, we will need a script that allows us to point at a set of coordinates, and move towards it.
Point towards (X, Y)

Mathematical form:
(([atan v] of ((x) / (y))) + ((180) * <(y) < [0]>))

Custom block form:
define point towards (x) (y) 
point in direction (([atan v] of (((x) - (x position)) / (((y) - (y position)) + (0)))) + ((180) * <(y) < (y position)>))


How this works:
The ‘atan’ operation takes the ratio of Y to X and produces the angle it would be at. Adding the (180 * condition) accounts for negative directions, so it would work for the full 360 degrees


Now, I will leave the rest to you to put the pieces together and find out how to make your AI.
=======================
2 - Diagonal Movement

editing later
I actually tested this script in a different test project to see if it would work and it did! However im not sure how i can apply it to my existing script for how the enemies work, theres a lot of parts to the script for the enemies so maybe you can look inside the test i just shared with my game and help me apply the script? https://scratch.mit.edu/projects/1388890085/
10goto10
Scratcher
1000+ posts

Need help with a moving script.

I started working on a movement case study before I saw that you had posted a link to your project. https://scratch.mit.edu/projects/1391281672/

I'm sharing here anyway incase there is something I've done that will help you or be of interest. The script that the player uses to control moving around allows diagonal movements and is a very small script. Also, maybe there is also something in the AI pathfinding script that you can use. There is so much tinkering that can be done to get the effect you want.

Powered by DjangoBB