Godot Version
4.7.2
Question
Hello! I am currently trying to figure out how to get my camera to “lead” the player in my 2D platformer, in other words, the camera moves ahead of the player. My old setup just took inputs from the keyboard and changed the camera’s position using lerp(). However, upon realizing that some situations may move the camera on it’s own, I decided I want to use the players direction instead, but I’ve had trouble producing the effect.
Below is the code for the player:
func _physics_process(delta: float) -> void:
var direction := Input.get_axis("left", "right") # Gets the input direction
# Decrease speed when mid-air
if not is_on_floor():
SPEED = 95
ACCEL = 300
elif is_on_floor():
SPEED = 100
ACCEL = 600
# Get the input direction and handle the movement/deceleration.
if direction:
velocity.x = move_toward(velocity.x, direction * SPEED, ACCEL * delta)
else:
velocity.x = move_toward(velocity.x, 0, ACCEL * delta)
# sprint
if Input.is_action_pressed("Sprint") and is_on_floor():
SPEED = 150
# Gravity
if not is_on_floor():
velocity += get_gravity() * delta
# jump + double jump (later)
# executes any time the player is grounded while the spacebar is held down
if Input.is_action_pressed("Jump") and is_on_floor():
velocity.y = JUMP_VELOCITY
# wall slide
if is_on_wall_only() and velocity.y > 0:
velocity.y = WALL_SPD
#wall jump
if is_on_wall_only() and Input.is_action_just_pressed("Jump"):
velocity.y = -200
velocity.x = get_wall_normal().x * 100
direction = get_wall_normal().x * 1
move_and_slide()
# makes the sprite flip
if direction == 1.0:
kiva_dir.flip_h = false
player_direction.emit()
elif direction == -1.0:
kiva_dir.flip_h = true
player_direction.emit()
I attempted to use the player_direction.emit()to tell the camera what direction the player is facing, but it didn’t work. Before anyone calls me stupid, I’m learning.