Every time I enter something cool into the script, the script again disagrees with the previous sessions. Now I have a problem because of the scripts—they’ve messed things up and caused errors.
Another error is related to my new revolution in updating the game, specifically the addition of the ability for objects to collide. I decided to repeat the entire script exactly as per the video training
, from start to finish, and unfortunately, an error occurred.
for i in get_slide_collision_count():
var collision = get_slide_collision(i)
var collider = collision.get_collider()
if collider is RigidBody3D:
collider.apply_central_impulse(-global_transform.basis.z * 2.0)
extends CharacterBody3D
var ORIGINAL_SPEED
var SPEED = 3.0
var run_drain_amount = 0.4
var run_refresh_amount = 0.3
var RUNNING_SPEED = 9.0
const JUMP_VELOCITY = 4.5
var run_slider
var movable = false
func _ready():
ORIGINAL_SPEED = SPEED
run_slider = get_node(“/root/” + get_tree().current_scene.name + “/UI/run_slider”)
func _process(delta):
if movable == true:
if SPEED == RUNNING_SPEED:
run_slider.value = run_slider.value - run_drain_amount * delta
if run_slider.value == run_slider.min_value:
SPEED = ORIGINAL_SPEED
$“head/Camera Animations”.play(“Walk”)
if SPEED != RUNNING_SPEED:
if run_slider.value < run_slider.max_value:
run_slider.value = run_slider.value + run_refresh_amount * delta
$“head/Camera Animations”.play(“Walk”)
if run_slider.value == run_slider.max_value:
run_slider.visible = false
func _physics_process(delta: float) → void:
# Add the gravity.
if not is_on_floor():
velocity += get_gravity() * delta
if movable == true:
# Handle jump.
if Input.is_action_just_pressed("jump") and is_on_floor():
velocity.y = JUMP_VELOCITY
# Get the input direction and handle the movement/deceleration.
# As good practice, you should replace UI actions with custom gameplay actions.
var input_dir := Input.get_vector("left", "right", "forward", "backward")
var direction := (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
if direction:
velocity.x = direction.x * SPEED
velocity.z = direction.z * SPEED
for i in get_slide_collision_count():
var collision = get_slide_collision(i)
var collider = collision.get_collider()
if collider is RigidBody3D:
collider.apply_central_impulse(-global_transform.basis.z * 2.0)
if Input.is_action_just_pressed("runnig"):
run_slider.visible = true
SPEED = RUNNING_SPEED
$"head/Camera Animations".play("Run")
if Input.is_action_just_released("runnig"):
SPEED = ORIGINAL_SPEED
$"head/Camera Animations".play("Walk")
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
$"head/Camera Animations".play("Idle")
velocity.z = move_toward(velocity.z, 0, SPEED)
$"head/Camera Animations".play("Walk")
move_and_slide()
I expected everything to work just like in the video, but it’s not the video script itself — it should work fine. Either my old script is outdated, or I might have accidentally deleted something.
Your else: is too indented, it matches the for loops indentation, instead of the if direction:'s indentation. Bump it to the left one.
it appears your pasted script only coincidentally had enough correct formatting so we could see indentation at the point of error, here’s another post on how to properly format code pastes
You’re right, I removed one indentation, but then all the other indentations I had at the bottom also lit up with an error. I checked and removed them; only one error “Else” remained lit. If I remove one more indentation in Else and delete the remaining ones, the error will be fixed, but a new one will appear in place of “If direction”.
Yes you will also have to un-indent other lines too. If you paste your script correctly I could be more concrete, but I’d wager every line up to and including move_and_slide() should be unindented left by one. If you have more errors, post your new edited script and let us know.
It’s still an indentation error, you didn’t post the error but given the var direction is highlighted yellow (probably unused variable) and the if direction is un-indented past it, your error message is likely “undefined variable direction”
Indentation in GDScript and Python controls the “scope” of statements. Adding indentation makes a new scope, variables defined in a scope only exist at that level of indentation and newer, more indented scopes. Your var direction is defined within the scope of if moveable == true:, but that scope ends by un-indentation and a new one begins with if direction:
It may be easiest to flip your moveable check, and do an “early return” which will stop the rest of the code from running
if moveable == false:
return # stops the rest of the function from running
Then un-indent your lines 36 through 43, inclusive.
It also appears you are checking for inputs inside of the for loop’s scope, you should un-indent lines 56 through 62 as well.
That would be another way to solve it, they would have to indent everything else below it too. Early returns can reduce the needed indentation, but it’s a stylistic choice.