• 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!

GameMaker Teleport to object

E

Elian

Guest
I need that when a key is pressed on the keyboard(Esc), the player teleports to the desired object...
Help please, I don't know what code to use for this(
 

Tyg

Member
On your keypress code

Player.x = Target.x;
Player.y = Target.y;

Target can be another objects x,y
or just use a hard coded number

Player.x = 450;
 
E

Elian

Guest
On your keypress code

Player.x = Target.x;
Player.y = Target.y;

Target can be another objects x,y
or just use a hard coded number

Player.x = 450;
what about create event of target?
 
E

Elian

Guest
What about it? You haven't said that anything else should happen. Tyg's code will work perfectly (for just teleporting).
The problem is nothing happens:
GML:
Obj_player.x = Obj_shield.x;
Obj_player.y = Obj_shield.y;
 

Tyg

Member
maybe your object doesnt exist
you should put
if object exists
where are you puting the code?
 
E

Elian

Guest
maybe your object doesnt exist
you should put
if object exists
where are you puting the code?
Yes, the object exists in the world
Code... obj_player -> key down escape (event) ->
Obj_player.x = Obj_shield.x;
Obj_player.y = Obj_shield.y;
 

Fielmann

Member
1) Are you sure that an instance of Obj_shield exists?
2) Are there no typos (e.g. obj_ instead of Obj_)?
3) Is there no other code that sets player's x and y (e.g. in the step event)?
 
Yes, the object exists in the world
Code... obj_player -> key down escape (event) ->
Obj_player.x = Obj_shield.x;
Obj_player.y = Obj_shield.y;
If you placed it in the obj_player's Key Down (ESC) event, then you can just code it;
Code:
x = obj_shield.x;
y = obj_shield.y;
Though I'd suggest placing it in Key Pressed (ESC) instead. And make sure that obj_shield has been placed in the room where the obj_player will be. And I think you'd also might want to have obj_player placed on a layer above obj_shield otherwise it'd be hidden behind it - then again depends how you want it to look. If in certain rooms there isn't going to be any obj_shield, then to avoid causing error should someone press ESC, you can also do this;
GML:
if instance_exists(obj_shield) {
    x = obj_shield.x;
    y = obj_shield.y;
}
If you're using GMS2.v.2.3.1.xxx, you can download this .yyz file and open it on your IDE : https://www118.zippyshare.com/v/9zF87Kio/file.html
If you press ESC, the player will move to the target position.
 
Last edited:
Top