This is the code on Game Maker. Make it so that the player collides with obstacles and…
- User
This is the code on Game Maker. Make it so that the player collides with obstacles and cannot pass through them.
/// @description
var _hsp = keyboard_check(vk_right) - keyboard_check(vk_left); var _vsp = keyboard_check(vk_down) - keyboard_check(vk_up);
x += _hsp * spd * blood; y += _vsp * spd * blood;
// Restrict movement during day if (global.is_day) { // Vampire can only be in caves (e.g., obj_cave area) if (place_meeting(x, y, obj_cave) == false) { // Vampire is exposed to sunlight hp = 0; // Vampire dies } }
- Assistant
To ensure that the player cannot pass through obstacles in your GameMaker project, you'll need to implement collision detection and response. A common and effective method is to handle horizontal and vertical movements separately, checking for collisions after each movement. This approach allows the player to slide along obstacles naturally.
Below is the modified code with collision handling against an obstacle object (
obj_obstacle). Make sure to replaceobj_obstaclewith the actual name of your obstacle object if it's different./// @description Player Movement with Collision Handling // Input Handling var _hsp = keyboard_check(vk_right) - keyboard_check(vk_left); var _vsp = keyboard_check(vk_down) - keyboard_check(vk_up); // Calculate movement increments var move_x = _hsp * spd * blood; var move_y = _vsp * spd * blood; // --- Horizontal Movement --- // Attempt to move horizontally x += move_x; // Check for collision with obstacles if (place_meeting(x, y, obj_obstacle)) { // Collision detected, revert horizontal movement x -= move_x; // Optionally, you can add sliding or other responses here // For example, stop horizontal movement if colliding // _hsp = 0; } // --- Vertical Movement --- if (_vsp != 0) { // Only attempt vertical movement if there's input y += move_y; // Check for collision with obstacles if (place_meeting(x, y, obj_obstacle)) { // Collision detected, revert vertical movement y -= move_y; // Optionally, you can add sliding or other responses here // For example, stop vertical movement if colliding // _vsp = 0; } } // --- Daytime Restrictions --- if (global.is_day) { // Check if the player is within a cave if (!place_meeting(x, y, obj_cave)) { // Player is exposed to sunlight and dies hp = 0; } } // --- Optional: Additional Collision Responses --- // You can add more complex collision responses here, such as sliding along walls, // triggering events when hitting certain objects, etc.Explanation of the Changes
-
Separate Horizontal and Vertical Movements:
-
Horizontal (
x) Movement:- Move: The player attempts to move horizontally by updating the
xposition. - Check Collision: After moving,
place_meetingchecks if the new position collides withobj_obstacle. - Revert if Colliding: If a collision is detected, the movement is undone by subtracting
move_xfromx.
- Move: The player attempts to move horizontally by updating the
-
Vertical (
y) Movement:- Move: Similarly, the player attempts to move vertically by updating the
yposition. - Check Collision: After moving,
place_meetingchecks for collisions. - Revert if Colliding: If a collision is detected, the movement is undone by subtracting
move_yfromy.
- Move: Similarly, the player attempts to move vertically by updating the
-
-
Optimizations and Enhancements:
- Conditional Vertical Movement: The vertical movement is only attempted if there is vertical input (
_vsp != 0), which can optimize performance slightly. - Optional Sliding: You can enhance the collision response to allow the player to slide along obstacles by not fully reverting the movement or by adjusting velocity variables accordingly.
- Collision with Different Objects: If you have multiple types of obstacles, you can modify the
place_meetingfunction to check against different objects or use layers.
- Conditional Vertical Movement: The vertical movement is only attempted if there is vertical input (
-
Daytime Restrictions:
- The existing logic to handle daytime restrictions remains unchanged. It ensures that during the day, the player (e.g., a vampire) must be within a cave (
obj_cave). If not, the player’s health (hp) is set to 0, resulting in death.
- The existing logic to handle daytime restrictions remains unchanged. It ensures that during the day, the player (e.g., a vampire) must be within a cave (
Additional Tips
-
Collision Masks: Ensure that your obstacle objects (
obj_obstacle) have proper collision masks set up. The collision mask determines howplace_meetingdetects collisions. -
Tile-Based Games: If your game uses tile maps for obstacles, consider using
tilemapfunctions liketilemap_get_at_pixelorcollision_linefor more efficient collision detection. -
Physics Engine: For more advanced movement and collision handling, you might explore using GameMaker’s built-in physics engine, which can handle more complex interactions automatically.
-
Debugging Collisions: Use
draw_rectangleordraw_spritein a debugging step to visualize collision areas and ensure that collision detection works as expected.
By implementing the above collision handling, your player should now interact properly with obstacles, preventing them from passing through walls or other solid objects in your game.
-