GMS 2.3+ Set player's X position to center of instance?

Pikpik

Member
I have an object that when the player collides with it, their x position should be set to the center of that specific instance. How would I accomplish this?
 

TsukaYuriko

šŸŒ 
Forum Staff
Moderator
I'll assume you mean the visual center of the assigned sprite.

If so, that depends on various factors. These are the instance's position and its assigned sprite's width, height and origin. You can incorporate these into a formula to find the visual center of the instance in regards to its drawn sprite.

Start at its position, add half of its sprite's width and height, respectively, then subtract the origin.
 

FoxyOfJungle

Kazan Games
GML:
x = inst.x + inst.sprite_width/2;
y = inst.y + inst.sprite_height/2;

// inst = the instance that you are colliding
// you can use "other", depends on how you are performing the collision. It also depends on other factors such as sprite alignment.
 

Pikpik

Member
I should have clarified that I have multiple instances of the object in the room. I want to make it so that the player snaps to the center of whatever instance of that object it touches.
 

TsukaYuriko

šŸŒ 
Forum Staff
Moderator
You can do that part via instance_place or any of the collision functions. Get the ID of the instance being collided with, then refer to its attributes using said ID.
 
Top