So i wanted to create a one-way platform for my game, I'm a beginner in GameMaker (and coding in general) , and new to StackOverflow so I don't know how to do it. I have my obj_player, with a create, and step events. I have an obj_oneway with no code attached to it.
So here's my create and step events for my player :
// Create Event
move_speed = 4; jump_speed = 16;
move_x = 0; move_y = 0;
tmWorld1 = layer_tilemap_get_id("tiles");
// Step Event
move_x = move_speed * sign(image_xscale); // Player will move in the direction it is facing
// Check if there is a wall in the direction the player is moving
if (place_meeting(x + move_x, y, tmWorld1)) {
image_xscale *= -1; // Flip the sprite and change direction
}
// Perform a custom collision check with obj_oneway
var oneway_id = instance_place(x + move_x, y, obj_oneway);
if (oneway_id && oneway_id.object_index == obj_oneway) {
// Check if the player is moving downward and is above the platform
if (move_y > 0 && y <= oneway_id.y) {
// Save the original collision mask
var original_mask = oneway_id.mask_index;
// Temporarily disable collisions from below
oneway_id.mask_index = 0;
// Apply movement
move_and_collide(move_x, move_y, tmWorld1);
// Restore the original collision mask
oneway_id.mask_index = original_mask;
}
else {
// If the player is not moving downward or is above, perform a normal collision check
if (place_meeting(x + move_x, y, tmWorld1)) {
// If there's a wall in front, stop horizontal movement
move_x = 0;
}
move_and_collide(move_x, move_y, tmWorld1);
}
}
else {
// If the player is not on a one-way platform, perform a normal collision check
if (place_meeting(x + move_x, y, tmWorld1)) {
// If there's a wall in front, stop horizontal movement
move_x = 0;
}
move_and_collide(move_x, move_y, tmWorld1);
}
// Check if there's ground below and handle jumping
if (place_meeting(x, y + 2, tmWorld1)) {
move_y = 0;
if (keyboard_check_pressed(vk_space)) { // Use keyboard_check_pressed to trigger jump only once
move_y = -jump_speed;
}
} else {
if (move_y < 10) {
move_y += 1;
}
}
I did the collision masks of the two sprites, a rectangle for the two of them. When I am under the platform, I can pass through it, but when I land on it, I can too, and I want the player to walk on the platform. On the tiles layer, there are some tiles, but the one way platform is not on this collision, it is an object and I saved it in the Instances layer. I have tried many things and I still don't understand why it does not work. If you have advices for my code I'll take them too. Thanks !
I think you're doing both tile collision and object collision here, though I've only experience with object collision. The platform is an object, so it may work out.
From what I remember from my platforming experience, I made the platform act as just a solid object, (a solid square that you can't pass through, you can do that by making the platform a child object from that solid object), and then used a button to temporaly ignore the platform object. (similair to what you do) The tricky part on my side was that the platform tile was much shorter than the solid tile, so it was possible that the character moves fast enough to "phase" through the platform inbetween 2 collision checks. So you might want to make sure that the platform is thick enough that you can't get past it by too much speed.
Another note is to double check on where the collision point happens. Which I like to visualise to draw a sprite on the collision point. (like a crosshair)