Trying to setup a Metroid style camera look ahead but only looks left

Godot Version

Godot 4

Question

I am patching my game and I am trying to put in a new feature so players can look left, right, up and down ahead of them to check for obstacles. Right now I was just trying to make the x offset but I can only get the player to look left and it’s a bit sudden. How can I make player look right as well and make it a bit less sudden?

This code is in my PlayerCamera2D that is a Camera2D node which my baselevel scene. My player has it’s own class name to be called in this script. I’ve tried switch how negative to positive it just stops working. I also have position smoothing enabled and set to 2 to try to smooth out the camera but I don´t see much from it.

@export var player : CharacterBody2D

func _physics_process(delta):
	if player != null:
		global_position = player.global_position
		#TRYING CAMERA LOOK AHEAD
		if Input.is_action_pressed("ui_accept") and Input.is_action_pressed("move_right"):
			player.velocity.x = 0
			player.speed = 0
			offset.x = player.position.x - 15
			
		elif Input.is_action_pressed("ui_accept") and Input.is_action_pressed("move_left"):
			player.velocity.x = 0
			player.speed = 0
			offset.x = player.position.x + 15
			
		else:
			offset.x = 0
			player.speed = 1000

Player script

class_name player
extends CharacterBody2D


signal respawned()

var bullet = preload("res://Player/bullet.tscn")
var player_death_effect = preload("res://Player/player_death_effect/player_death_effect.tscn")


@onready var muzzle : Marker2D = $Muzzle
#@onready var hit_animation_player = $HitAnimationPlayer
@onready var animated_sprite_2d = $AnimatedSprite2D
@onready var jump_sound = $Sounds/Jump
@onready var run_sound = $Sounds/Run
@onready var death_sound = $Sounds/Death
@onready var damage_sound = $Sounds/Damage
@onready var respawn_sound = $Sounds/Respawn
@onready var actionable_finder = $ActionableFinder


const GRAVITY = 1000
const SPEED = 1000
@export var speed : int = SPEED
@export var max_horizontal_speed: int = 300
@export var slow_down_speed : int = 1500


@export var jump : int = -300
@export var jump_horizontal_speed : int = 1000
@export var max_jump_horizontal_speed : int = 300
@export var jump_height :  float = -250
#change value below for multiple jumps
@export var jump_count : int = 1 
@export var max_jump_count : int = 2
@export var character_body_2d : CharacterBody2D


@export var knockbackPower : int = 500
@export var enemy_position : Vector2

@export var dialogue_resource : DialogueResource 
@export var dialogu_start : String = "Health_states"


#for save 
@export var health_for_save : int = HealthManager.current_health
@export var xeno_for_save : Dictionary = CollectibleManager.xeno_collected
@export var inventory_for_save : Dictionary = InventoryManager.inventory



enum State {Idle, Run, Jump, Shoot, Falling, Damage, Jump_side}

var current_state : State
var muzzle_position
var current_jump_count : int
var character_sprite : Sprite2D
var coyote_jump : bool
var is_dead : bool
var checkpoint_manager


func _ready():
	#current_state = State.Idle
	
	
	muzzle_position = muzzle.position
	checkpoint_manager = get_parent().get_node("CheckpointManager")
	SceneManager.on_trigger_player_spawn.connect(_on_spawn)
	
	
	
	
func _on_spawn(position: Vector2, direction: String):
	global_position = position
	
	current_state = State.Run
	HealthManager.change_health(health_for_save)
	#print_debug("Health for save", health_for_save)
	InventoryManager.inventory = inventory_for_save.duplicate(true)
	#print_debug("inventory", inventory_for_save)
	CollectibleManager.xeno_collected = xeno_for_save.duplicate(true)
	#print_debug("xeno on player", xeno_for_save)
	
	CollectibleManager.on_collectible_award_received.emit(xeno_for_save.size())
	CollectibleManager.total_award_amount = xeno_for_save.size()
	
	


#func _unhandled_input(event:InputEvent) -> void:
	#if Input.is_action_just_pressed("Interact"):
		#var actinables = actionable_finder.get_overlapping_areas()
		#if actinables.size()>0:
			#actinables[0].action()
			#return 




func _physics_process(delta: float):
	player_falling(delta)
	player_idle(delta)
	player_run(delta)
	player_jump(delta)
	
	player_muzzle_position()
	player_shoting(delta)
	move_and_slide()
	player_animations()
	
	
	
	#print("State:", State.keys()[current_state])


func player_falling(delta: float):
	if !is_on_floor():
		velocity.y += GRAVITY * delta
		current_state = State.Falling
		

func player_idle(delta: float):
	var direction = Input_movement()
	if is_on_floor() and direction == 0:
		current_state = State.Idle
		


func player_run(delta: float):
	var direction = Input_movement()
	
	
	if direction:
		velocity.x += direction * speed * delta
		velocity.x = clamp(velocity.x, -max_horizontal_speed, max_horizontal_speed)
	else:
		velocity.x = move_toward(velocity.x, 0, slow_down_speed * delta)
	
	if direction != 0 and HealthManager.current_health != 0:
		if is_on_floor():
			current_state = State.Run
			run_sound.play()
		

		animated_sprite_2d.flip_h = false if direction > 0 else true
	if direction == 0:
		run_sound.stop()

func player_jump(delta: float):
	var jump_input : bool = Input.is_action_just_pressed("jump")
	var direction = Input_movement()
	
	if is_on_floor() and jump_input:
		current_jump_count = 0
		velocity.y = jump_height
		coyote_jump = false
		current_jump_count += 1
		current_state = State.Jump
		jump_sound.play()
	
	if coyote_jump:
		character_body_2d.velocity.y = jump_height
		coyote_jump = false
		current_jump_count += 1
		current_state = State.Jump_side
		
		# multiple jumps
	if !is_on_floor() and jump_input and current_jump_count != max_jump_count:
		character_body_2d.velocity.y = jump_height
		current_jump_count += 1
		current_state = State.Jump_side
		jump_sound.play()
		
	
	
	if !is_on_floor() and current_state == State.Jump:
		velocity.x += direction * jump_horizontal_speed * delta
		velocity.x = clamp(velocity.x, -max_jump_horizontal_speed, max_jump_horizontal_speed)
		
	if !is_on_floor() and Input.get_axis("move_left", "move_right"):
		current_state = State.Jump_side
	
	animated_sprite_2d.flip_h = true if direction > 0 else false


func player_shoting(delta:float):
	var direction = Input_movement()
	
	if direction != 0 and Input.is_action_just_pressed("shoot"):
		var bullet_instance = bullet.instantiate() as Node2D
		bullet_instance.direction = direction
		bullet_instance.global_position = muzzle.global_position 
		get_parent().add_child(bullet_instance)
		current_state = State.Shoot

func player_muzzle_position():
	var direction = Input_movement()
	
	if direction > 0:
		muzzle.position.x = muzzle_position.x
	elif direction < 0 :
		muzzle_position.x = -muzzle_position.x


func player_animations():
	if current_state == State.Idle:
		animated_sprite_2d.play("idle")
		
	elif current_state == State.Run and animated_sprite_2d.animation != "Run_shoot":
		animated_sprite_2d.play("Run")
	elif current_state == State.Jump:
		animated_sprite_2d.play("Jump")
		
	elif current_state == State.Falling:
		animated_sprite_2d.play("Fall")
		
	elif current_state == State.Damage:
		animated_sprite_2d.play("Damage")
	
	elif current_state == State.Jump_side:
		animated_sprite_2d.play("Jump_side")
		
	elif current_state == State.Shoot:
		animated_sprite_2d.play("Run_shoot")


func player_death():
	var player_death_effect_instance = player_death_effect.instantiate() as Node2D
	if is_dead == true:
		#print_debug("calling player death func")
		player_death_effect_instance.global_position = global_position
		get_parent().add_child(player_death_effect_instance)
		death_sound.play()
		DialogueManager.dialogue_ended
		visible = false
		set_process_input(false)
		set_process_unhandled_input(false)
		set_process(false)
		set_physics_process(false)
	
		await get_tree().create_timer(2).timeout
		get_tree().paused = true
		
		#print_debug("BEFORE called respawn")
		respawned.emit()
		is_dead = false
		HealthManager.increase_health(3)
		velocity.x = 0
		global_position = checkpoint_manager.last_location
		respawn_sound.play()
		get_tree().paused = false
		
	
	#get_tree().reload_current_scene()
		visible = true
		set_process_input(true)
		set_process_unhandled_input(true)
		set_process(true)
		set_physics_process(true)
	
		if DialogueManager.dialogue_ended:
			DialogueManager.show_dialogue_balloon(dialogue_resource, dialogu_start)
		if DialogueManager.dialogue_started:
			pass

	
	if is_dead == false:
		pass
	
	

func Input_movement():
	var direction : float = Input.get_axis("move_left", "move_right")
	
	return direction


func _on_hurtbox_body_entered(body : Node2D):
	if body.is_in_group("Enemy"):
		if is_dead == false:
			enemy_position = body.global_position
			current_state = State.Damage
			var tween = get_tree().create_tween()
			tween.tween_property(animated_sprite_2d, "material:shader_parameter/enabled", true, 0)
			tween.tween_property(animated_sprite_2d, "material:shader_parameter/enabled", false, 0.2)
			damage_sound.play()
			
		
			HealthManager.decrease_health(body.damage_amount)
			knockback(1,1500,300)
		if is_dead == true:
			pass
	
	if body.is_in_group("ouchy"):
		if is_dead == false:
			current_state = State.Damage
			var tween = get_tree().create_tween()
			tween.tween_property(animated_sprite_2d, "material:shader_parameter/enabled", true, 0)
			tween.tween_property(animated_sprite_2d, "material:shader_parameter/enabled", false, 0.2)
			damage_sound.play()
			
		
			HealthManager.decrease_health(1)
			knockback(0.5,500,400)
		if is_dead == true:
			pass
		
		
	
	
	if HealthManager.current_health == 0:
		if is_dead == false:
			is_dead = true
			
			player_death()
		if is_dead == true:
			
			pass
			
		
	
	
func knockback(force: float, y_pos: float, up_force: float):
	if (character_body_2d.position.y - enemy_position.y > 0):
		character_body_2d.velocity = Vector2(-force * 2 * y_pos, -force * up_force)
		#print(enemy_position.x)
		#print(character_body_2d.global_position.x)
	else: 
		character_body_2d.velocity = Vector2(force * 2 * y_pos, -force * up_force)
	
	
	

Are you moving your camera by something other than offset.x? Maybe you shouldn’t be using player.position.x for the offset value, 15 doesn’t sound like a lot to offset by.

Your class_name player should be capitalized (to class_name Player) to prevent confusion between the Class and any instance of it.

Not that I am aware of. All I have setting the camera is setting it to the player’s global position by default´and it’s limits on the inspector (not by code) and then the offset that I’m trying to setup on the inputs.

oh thanks! didn’t know that, gonna change for best practice sack.

Found a solution that works:

func _physics_process(delta: float) -> void :
	if player == null:
		return
	if player != null:
		global_position = player.global_position
		#TRYING CAMERA LOOK AHEAD
		if Input.is_action_pressed("ui_accept") and Input.is_action_pressed("move_right"):
			var dir := Vector2.RIGHT
			var vel = player.get("velocity")  # null if the target has no velocity
			player.velocity = Vector2.ZERO

			if vel is Vector2:
				dir = vel.normalized()
				offset = offset.lerp(dir * look_ahead, look_speed * delta)
				print_debug("right", offset)
				print_debug("right", player.global_position)

		elif Input.is_action_pressed("ui_accept") and Input.is_action_pressed("move_left"):
			var dir := Vector2.LEFT
			var vel = player.get("velocity")  # null if the target has no velocity
			player.velocity = Vector2.ZERO

			if vel is Vector2:
				dir = vel.normalized()
				offset = offset.lerp(dir * look_ahead, look_speed * delta)
				print_debug("left", offset)
		elif Input.is_action_pressed("ui_accept") and Input.is_action_pressed("look_down"):
			var dir := Vector2.DOWN
			var vel = player.get("velocity")  # null if the target has no velocity
			if vel is Vector2:
				#dir = vel.normalized()
				offset = offset.lerp(dir * look_ahead, look_speed * delta)
				print_debug("down", offset)
				print_debug("down", player.global_position)
		elif Input.is_action_pressed("ui_accept") and Input.is_action_pressed("look_up"):
			var dir := Vector2.UP
			var vel = player.get("velocity")  # null if the target has no velocity
			if vel is Vector2:
				#dir = vel.normalized()
				offset = offset.lerp(dir * look_ahead, look_speed * delta)
				print_debug("up", offset)
				print_debug("up", player.global_position)
		else:
			offset = Vector2.ZERO