error: If you press e up close, objBox flies upward.
//Create Event of objBox
isHeld = false; // Indicates if the box is being carried
heldBy = noone; // Reference to the player instance holding the box
//Step Event of objBox
if (isHeld && instance_exists(heldBy)) {
var player = heldBy;
code
// Define offsets based on player's direction
var offsetX = player.image_xscale * 40; // 40 pixels to the side
var offsetY = -sprite_get_height(player.sprite_index) / 2; // Adjust vertically
// Smoothly follow the player (optional)
x = lerp(x, player.x + offsetX, 0.5);
y = lerp(y, player.y + offsetY, 0.5);
}
// part of Create Event of player object
carryingBox = false;
heldBox = noone;
// part of playerStep()
if (keyboard_check_pressed(ord("E"))) {
if (!carryingBox) {
// Attempt to pick up a box
var box = instance_nearest(x, y, objBox);
var pickupRange = 32;
code
if (box != noone && point_distance(x, y, box.x, box.y) <= pickupRange && !box.isHeld) {
carryingBox = true;
heldBox = box;
box.isHeld = true;
box.heldBy = id;
}
} else {
// Attempt to put down the box
if (heldBox != noone) {
var dropX = x + image_xscale * 40;
var dropY = y - sprite_get_height(sprite_index) / 2;
// Check for collision at drop position
if (!place_meeting(dropX, dropY, objSolid) && !place_meeting(dropX, dropY, objTopSolid)) {
// Place the box
heldBox.x = dropX;
heldBox.y = dropY;
heldBox.isHeld = false;
heldBox.heldBy = noone;
// Update carrying state
carryingBox = false;
heldBox = noone;
}
}
}
}
Assistant
The issue you're encountering—where pressing E causes the objBox to fly upward when it's up close—is likely due to how the offsetY is calculated and applied in the Step Event of objBox. Let's break down the problem and provide a comprehensive solution.
Understanding the Problem
Offset Calculation:
gml
var offsetY = -sprite_get_height(player.sprite_index) / 2; // Adjust vertically
Issue: Using a negative offset (-sprite_get_height / 2) positions the objBoxabove the player's Y-coordinate. If the player's sprite height is significant, this can cause the box to appear to "fly upward" relative to the player.
Lerping Position:
gml
y = lerp(y, player.y + offsetY, 0.5);
Issue: The lerp function smoothly transitions the box's Y-position towards the target (player.y + offsetY). If offsetY is negative, the box continuously moves upward towards a position above the player, exacerbating the flying effect.
Dropping the Box:
gml
var dropY = y - sprite_get_height(sprite_index) / 2;
Issue: Similar to the Step Event, subtracting half the sprite height positions the box above the player when dropping, which might not be the intended behavior.
Proposed Solutions
1. Adjust the offsetY Calculation
Instead of using a negative offset, use a positive value to position the box below the player or at a desired relative position.
Modify the Step Event of objBox:
gml
// Define offsets based on player's direction
var offsetX = player.image_xscale * 40; // 40 pixels to the side
var offsetY = sprite_get_height(player.sprite_index) / 2; // Adjust vertically below the player
// Smoothly follow the player (optional)
x = lerp(x, player.x + offsetX, 0.5);
y = lerp(y, player.y + offsetY, 0.5);
2. Update the Box Dropping Logic
Ensure that when the box is dropped, it's positioned correctly relative to the player without being offset upward.
Modify the Drop Section in playerStep():
gml
// Attempt to put down the box
if (heldBox != noone) {
var dropX = x + image_xscale * 40;
var dropY = y + sprite_get_height(sprite_index) / 2; // Position below the player
// Check for collision at drop position
if (!place_meeting(dropX, dropY, objSolid) && !place_meeting(dropX, dropY, objTopSolid)) {
// Place the box
heldBox.x = dropX;
heldBox.y = dropY;
heldBox.isHeld = false;
heldBox.heldBy = noone;
// Update carrying state
carryingBox = false;
heldBox = noone;
}
}
Change:dropY is now calculated with a positive offset, positioning the box below the player.
3. Optional: Remove or Adjust Smooth Following
If the smooth following (using lerp) causes undesirable behavior, consider removing it or adjusting the lerp factor.
Remove Smooth Following:
gml
// Directly attach the box to the player without smoothing
x = player.x + offsetX;
y = player.y + offsetY;
Adjust Lerp Factor:
A smaller lerp factor (closer to 0) makes the box follow more slowly, while a factor closer to 1 makes it follow more tightly.
gml
y = lerp(y, player.y + offsetY, 0.3); // Slower following
4. Ensure Consistent Coordinate Systems
Double-check that the player's Y-coordinate represents the position you intend (e.g., feet vs. center of the sprite). This ensures the box aligns correctly when picked up and dropped.
Revised Code Snippets
objBox Create Event:
gml
isHeld = false; // Indicates if the box is being carried
heldBy = noone; // Reference to the player instance holding the box
objBox Step Event:
gml
if (isHeld && instance_exists(heldBy)) {
var player = heldBy;
// Define offsets based on player's direction
var offsetX = player.image_xscale * 40; // 40 pixels to the side
var offsetY = sprite_get_height(player.sprite_index) / 2; // Adjust vertically below the player
// Smoothly follow the player (optional)
x = lerp(x, player.x + offsetX, 0.5);
y = lerp(y, player.y + offsetY, 0.5);
}
Player Object Create Event:
gml
carryingBox = false;
heldBox = noone;
Player Step Event (playerStep()):
gml
if (keyboard_check_pressed(ord("E"))) {
if (!carryingBox) {
// Attempt to pick up a box
var box = instance_nearest(x, y, objBox);
var pickupRange = 32;
if (box != noone && point_distance(x, y, box.x, box.y) <= pickupRange && !box.isHeld) {
carryingBox = true;
heldBox = box;
box.isHeld = true;
box.heldBy = id;
}
} else {
// Attempt to put down the box
if (heldBox != noone) {
var dropX = x + image_xscale * 40;
var dropY = y + sprite_get_height(sprite_index) / 2; // Position below the player
// Check for collision at drop position
if (!place_meeting(dropX, dropY, objSolid) && !place_meeting(dropX, dropY, objTopSolid)) {
// Place the box
heldBox.x = dropX;
heldBox.y = dropY;
heldBox.isHeld = false;
heldBox.heldBy = noone;
// Update carrying state
carryingBox = false;
heldBox = noone;
}
}
}
}
Additional Recommendations
Visual Feedback:
Consider adding animations or visual indicators to show when the box is being carried or dropped.
Error Handling:
Ensure that there are no conflicting events or physics that might interfere with the box's position when held.
Testing:
After implementing the changes, thoroughly test the pickup and drop mechanics in various scenarios to ensure stability.
By adjusting the offsetY to position the box appropriately relative to the player and ensuring that the box's position isn't inadvertently being set above the player, you should eliminate the "flying upward" issue. Additionally, refining the smooth following behavior can help achieve the desired responsiveness.