Discuss Scratch
- Discussion Forums
- » Help with Scripts
- » Glide Block Alternative.
- I_dont_eat_Money
-
Scratcher
30 posts
Glide Block Alternative.
I'm trying to make a tower defense game, And right now I'm trying to get the enemies to follow a set path. I have a list with pairs of x and y coordinates, which are placed so that the enemy can move in a perfectly straight lines from point to point without looking janky. However, I want the enemy to move at a set speed rather than just taking a set time to move from point to point (Glide Block), because all the points are at different distances than each other. I tried employing this kind of strategy:
But then if the distance x needs to move is not the same a y or vice versa. So if I wanted to move to a point with a slightly lower y but much farther x, the sprite would quickly move down, stop, and go the full y distance, which could look pretty janky.
So what I'm saying is, I need some exact block substitution for the “glide to x, y” block, except using a set speed rather than duration.
This was really difficult to explain.
if <(X) < (Destination X)> then
change x by (Speed)
end
But then if the distance x needs to move is not the same a y or vice versa. So if I wanted to move to a point with a slightly lower y but much farther x, the sprite would quickly move down, stop, and go the full y distance, which could look pretty janky.
So what I'm saying is, I need some exact block substitution for the “glide to x, y” block, except using a set speed rather than duration.
This was really difficult to explain.
- groko13
-
Scratcher
100+ posts
Glide Block Alternative.
One solution would be to solve for the glide duration based on the speed and distance to the next point. For example:
This assumes that the speed is in units per second (so, a speed of 10 could move 10 units in either the x or y direction in 1 second). I'm not sure if that makes sense, but let me know if that helps (or if you have any questions).
set [Distance X v] to ((Destination X) - (x position))//calculates distance to point in x-direction
set [Distance Y v] to ((Destination Y) - (y position))//calculates distance to point in y-direction
set [Total distance v] to ([sqrt v] of (((Distance X) * (Distance X)) + ((Distance Y) * (Distance Y))))//calculates straight-line distance
set [time v] to ((Total distance) / (Speed))//calculates glide time
glide (time) secs to x: (Destination X) y: (Destination Y)
This assumes that the speed is in units per second (so, a speed of 10 could move 10 units in either the x or y direction in 1 second). I'm not sure if that makes sense, but let me know if that helps (or if you have any questions).
- I_dont_eat_Money
-
Scratcher
30 posts
Glide Block Alternative.
One solution would be to solve for the glide duration based on the speed and distance to the next point. For example:set [Distance X v] to ((Destination X) - (x position))//calculates distance to point in x-direction
set [Distance Y v] to ((Destination Y) - (y position))//calculates distance to point in y-direction
set [Total distance v] to ([sqrt v] of (((Distance X) * (Distance X)) + ((Distance Y) * (Distance Y))))//calculates straight-line distance
set [time v] to ((Total distance) / (Speed))//calculates glide time
glide (time) secs to x: (Destination X) y: (Destination Y)
This assumes that the speed is in units per second (so, a speed of 10 could move 10 units in either the x or y direction in 1 second). I'm not sure if that makes sense, but let me know if that helps (or if you have any questions).
Wow, Thanks!
- Discussion Forums
- » Help with Scripts
-
» Glide Block Alternative.