Skip to content

Instantly share code, notes, and snippets.

@PurpBatBoi
Created December 31, 2025 17:53
Show Gist options
  • Select an option

  • Save PurpBatBoi/08f6d4530c6a075fc157e5a880dd0d51 to your computer and use it in GitHub Desktop.

Select an option

Save PurpBatBoi/08f6d4530c6a075fc157e5a880dd0d51 to your computer and use it in GitHub Desktop.
Makes the camera "look ahead" depending on the direction the character is moving
local lookAheadCam = {}
local handycam = require("handycam")
local ahead_offset_state = {}
function lookAheadCam.onCameraUpdate(idx)
local hcam = handycam[idx]
local plr = Player(idx)
local plr_X = plr.x + plr.width * 0.5
local lookaheaddistance = 96 -- Alter this value to increase/decrease lookahead distance
local dragSpeed = 0.2
local targetOffset = 0
if not ahead_offset_state[idx] then
ahead_offset_state[idx] = {
offset_ahead = 0
}
end
local state = ahead_offset_state[idx]
if plr.speedX ~= 0 then -- Check if the player is moving
if plr.direction == -1 then
targetOffset = -lookaheaddistance -- Look ahead to the left
else
targetOffset = lookaheaddistance -- Look ahead to the right
end
end
state.offset_ahead = state.offset_ahead + (targetOffset - state.offset_ahead) * dragSpeed
local targetX = plr_X + state.offset_ahead
hcam.x = hcam.x + (targetX- hcam.x) * dragSpeed
end
function lookAheadCam.onCameraDraw()
local centerX = camera.width / 2
local centerY = camera.height / 2
Graphics.drawBox{ x = centerX - 2, y = 0, w = 4, h = camera.height, color = Color.red .. 0.5 }
end
-- Delete this block above in order to remove the Red Rectangle
function lookAheadCam.onInitAPI()
registerEvent(lookAheadCam, "onCameraDraw")
registerEvent(lookAheadCam, "onCameraUpdate")
end
return lookAheadCam
-- This is released on the MIT LICENSE (https://opensource.org/license/mit)
-- TL-DR:
-- You are free to use and modify this code in any way you like,
-- as long as you include the original license and copyright notice.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment