Why doesn't my camera script work?

Godot Version

extends Node

@onready var camera_2d: Camera2D = $“.”
var playery
var playerx
@onready var character_body_2d: CharacterBody2D = $“../CharacterBody2D”

func _physics_process(delta: float) → void:
playerx = character_body_2d.position.x/1152
ceil(playerx)
playery = character_body_2d.position.y/648
ceil(playery)
print(playerx)
print(playery)
camera_2d.position.x =playerx1152-1152
camera_2d.position.y =playery
648-648

Question

Why doesn’t this work? This is supposed to make the camera jump to a place every time the player gets there but instead it just permanently follows the player around? Also the print isn’t printing in the output section (Yes I have standard output messages visible)

if you are setting the camera’s position to the player’s position every frame via _physics_process then it’s going to follow the player every frame.

It seems like you are re-implementing the snappedf function, but missed a crucial step where ceil does not change the variable it’s operating on, only a copy is returned. you would need to set it equal to the result.

playerx = ceil(playerx)

Or use the snapped function.

var snap_step := Vector2(1152, 648)
var player_pos := character_body_2d.position.snapped(snap_step)
camera_2d.position = player_pos

Make sure to paste code with proper formatting.

I think you also need to set the cameratype to scriptable. Cameratype.scriptable

What are you talking about? :thinking:

Edit: I see. This is Godot not Roblox.

:laughing: apologies

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.