I don’t have much idea about the movement code itself because I haven’t really dabbled with 3D stuff in Godot yet, but I can confidently tell you that your main problem is the fact that this script does not know what camera is.
The easiest way to get a reference to the camera is to drag the camera node from the scene tree into the script itself, making sure to hold ctrl (cmd on Mac) before dropping the camera node in the script, but not when going to drag the node. It will automatically create a reference to the camera for you to use in your script.
Depending on how your scene tree is organized, it might look a little something like this:
@onready var camera = $"Camera"
It may or may not look exactly like this, but this should work.
The reason you need to do this is actually hinted at in the error you’re getting, and it might be helpful to look into what “scope” means in case you ever run into similar issues.
To put it simply: “Scope” is like a stingy box that can receive items from the outside, but will not give items to the outside.
For example, consider the following code
var outside_variable: int = 5
func _ready():
print(outside_variable)
some_function()
print(inside_variable)
func some_function():
var inside_variable: String = "This is MY string only!"
print(outside_variable)
print(inside_variable)
When the _ready function is called, the script will run through each function and act accordingly. It will successfully run the first print function, outputting 5 to the terminal, then it will run some_function, which declares and sets inside_variable, prints 5 to the terminal, and then prints This is MY string only! to the terminal. Everything has worked fine so far, but problems arise when it tries to run that last print function in the _ready function.
The _ready function simply does not have access to inside_variable, and thus will throw an error. some_function is being greedy and refuses to let anyone outside of the Scope of the function access the variable.
If inside_variable was instead declared with outside_variable, everything would work just fine, and some_function wouldn’t be able to gate-keep the variable.
To shut down some confusion, the reason why your script can access stuff like velocity or is_on_floor() just fine, even though you haven’t declared any of them in your script, is that they comes with the CharacterBody3D class, which gets them from some other parent class.
Scope can be somewhat tricky to wrap your head around (I’d be lying if I said it hasn’t tripped me up a few times in the past), but as you refine your skills and do some more programming, considering the Scope of a variable or function will start to become second nature.