Bentley
Member
GM Version: GMS 1.4 / GM2
Target Platform: ALL
Download: N/A
Links: [email protected]
Summary
This tutorial is mainly about climbing slopes using a simple approach. When we get to a slope, measure the slope and compare the slope to how high you can climb. In addition, there are platformer basics like acceleration, jumping, setting sprites, and so forth. An example of what I'm talking about is in the GIF below (Yoshi's Island sprite).
Tutorial
I drew a randomly shaped sprite, checked precise collision mask, and then assigned it to obj_level. All the terrain is obj_level and it is what I collision check against.
approach script - moves one value to another value
obj_player
Create Event
[Step Event]
Slope scripts
[up_slope]
[down_slope]
In both scripts, "dy" will be the exact distance to climb or 1 greater. That number is fed into: if (dy <= slope_max), which determines whether we can climb.
Target Platform: ALL
Download: N/A
Links: [email protected]
Summary
This tutorial is mainly about climbing slopes using a simple approach. When we get to a slope, measure the slope and compare the slope to how high you can climb. In addition, there are platformer basics like acceleration, jumping, setting sprites, and so forth. An example of what I'm talking about is in the GIF below (Yoshi's Island sprite).
Tutorial
I drew a randomly shaped sprite, checked precise collision mask, and then assigned it to obj_level. All the terrain is obj_level and it is what I collision check against.
approach script - moves one value to another value
GML:
if (argument0 < argument1) return min(argument0 + argument2, argument1);
return max(argument0 - argument2, argument1);
Create Event
GML:
hspd = 0;
vspd = 0;
hspd_max = 3;
vspd_max = 6;
slope_max = 8; // Max climb distance
accel = 0.2;
frict = 0.2;
grav = 0.4;
[Step Event]
GML:
var kright, kleft, kjump, xdir, onground;
kright = keyboard_check(vk_right);
kleft = keyboard_check(vk_left);
kjump = keyboard_check_pressed(vk_up);
xdir = keyboard_check(vk_right) - keyboard_check(vk_left);
onground = place_meeting(x, y + 1, o_level);
// Handle horizontal input
if (xdir != 0)
{
if (onground) sprite_index = spr_player_run;
image_xscale = xdir;
hspd = approach(hspd, hspd_max * xdir, accel);
}
else
{
if (onground) sprite_index = spr_player_idle;
hspd = approach(hspd, 0, fric);
}
// Jump
if (kjump && onground)
{
sprite_index = spr_player_jump;
vspd = -vspd_max;
}
// Gravity
if (!onground)
{
if (vspd > 0) sprite_index = spr_player_fall;
vspd = approach(vspd, vspd_max, grav);
}
// Actually move
var dy;
repeat (abs(hspd)) // Horizontal movement
{
if (onground) // Check for slopes if we are on the ground
{
if (place_meeting(x + sign(hspd), y, obj_level)) // Up slope
{
dy = up_slope(); // Measure the slope
if (dy <= slope_max) // Climbable
{
x += sign(hspd);
y -= dy;
continue;
}
hspd = 0; // Unclimbable
break;
}
else if (!place_meeting(x + sign(hspd), y + 1, obj_level)) // Down slope (just like we did up slopes)
{
dy = down_slope();
if (dy <= slope_max)
{
x += sign(hspd);
y += dy;
continue;
}
x += sign(hspd); // Continue the loop rather than break the loop to keep momentum
continue;
}
else x += sign(hspd); // Flat
}
else // We're in the air so we don't check for slopes
{
if (!place_meeting(x + sign(hspd), y, obj_level)) x += sign(hspd);
else
{
hspd = 0;
break;
}
}
}
repeat (abs(vspd)) // Vertical movement
{
if (!place_meeting(x, y + sign(vspd), obj_level)) y += sign(vspd);
else
{
vspd = 0;
break;
}
}
[up_slope]
GML:
/// @function up_slope()
/// @desc This script will tell us how high the slope is.
var dy = 0;
while (place_meeting(x + sign(hspd), y - dy, obj_level)) // While we are not at the top of the slope
{
dy++;
if (dy > slope_max) break; // Break if the slope is greater than we can climb
}
return dy;
GML:
/// @funct down_slope()
/// @desc This script will tell us how low the slope is.
var dy = 0;
while (!place_meeting(x + sign(hspd), y + dy + 1, obj_level)) // While we are not at the bottom of the slope
{
dy++;
if (dy > slope_max) break; // Break if the slope is greater than we can climb
}
return dy;
Last edited: