• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

GML Teleportation

hgiabao

Member
Hi, I'm new to GMS, I've been using GMS for 3 months.
In my game, the main character has teleport power. When the player points the mouse to any position on the screen, the character will teleport to that position, there are obstacles that will block the teleportation so the character will stop in front of that object, but there are also objects such as enemies, the character can teleport through that. Is there a specific line of code to do it? If you can, please help me add the teleport effect too, if not, that's fine. Thanks for your help!
 

Alice

Darts addict
Forum Staff
Moderator
I think collision_line might be a good start - it allows to check whether objects of specific type are along the way, so if you determine that there are no obstacles to teleportation, you can teleport there just like that.
It doesn't take into account the player's size - just the line itself. So you might want to do multiple collision line check (e.g. each for the player collision box corner). Or you might just check the player's center and make it a quality of life feature that the player can teleport to the target "grazing" the obstacles along the way, as long as the player can spawn in their entirety at the destination (you can use place_meeting for that check).

If there *are* obstacles, things become trickier. You might want to do multiple collision_line checks, adjusting the length to be shorter if there are still obstacles, or to be longer (but shorter than known colliding length) if no obstacles were found.

It could be pretty similar to binary search, I guess? So e.g.:
1. Check collision_line across the entire length.
- if there are no obstacles and player can teleport to the spot, teleport there and stop
- if there is an obstacle, keep going
2. Set the known unobstructed length to 0, and known obstructed length to full length.
3. In each step, check for the midpoint between the unobstructed and obstructed length. So e.g. if it's from unobstructed 0 to obstructed full length, then check collision_line for half length.
- if there are no obstacles, then set the unobstructed length to just checked length
- if there are obstacles, then set the obstructed length to just checked length
4. Repeat step 3 until the difference between the unobstructed length and obstructed length is acceptably small. Also, make sure the player with their collision box can teleport to the found destination point.

This is a very rough idea, but I hope you can figure things out from there.
 

hgiabao

Member
I think collision_line might be a good start - it allows to check whether objects of specific type are along the way, so if you determine that there are no obstacles to teleportation, you can teleport there just like that.
It doesn't take into account the player's size - just the line itself. So you might want to do multiple collision line check (e.g. each for the player collision box corner). Or you might just check the player's center and make it a quality of life feature that the player can teleport to the target "grazing" the obstacles along the way, as long as the player can spawn in their entirety at the destination (you can use place_meeting for that check).

If there *are* obstacles, things become trickier. You might want to do multiple collision_line checks, adjusting the length to be shorter if there are still obstacles, or to be longer (but shorter than known colliding length) if no obstacles were found.

It could be pretty similar to binary search, I guess? So e.g.:
1. Check collision_line across the entire length.
- if there are no obstacles and player can teleport to the spot, teleport there and stop
- if there is an obstacle, keep going
2. Set the known unobstructed length to 0, and known obstructed length to full length.
3. In each step, check for the midpoint between the unobstructed and obstructed length. So e.g. if it's from unobstructed 0 to obstructed full length, then check collision_line for half length.
- if there are no obstacles, then set the unobstructed length to just checked length
- if there are obstacles, then set the obstructed length to just checked length
4. Repeat step 3 until the difference between the unobstructed length and obstructed length is acceptably small. Also, make sure the player with their collision box can teleport to the found destination point.

This is a very rough idea, but I hope you can figure things out from there.
Although I am new and may not fully understand, I really appreciate your answer.
 
Top