Bat doesnt fly dispite code

Godot Version

4.3

Question

I following a tutorial and his bat is flying but mine doesnt anyone know why
extends CharacterBody2D

const speed = 30
var dir: Vector2

var is_bat_chase: bool

func _ready():
is_bat_chase = false

func _process(delta):
move(delta)
handle_animation()

func move(delta):
if !is_bat_chase:
velocity += dir * speed * delta
move_and_slide()

func _on_timer_timeout() → void:
$Timer.wait_time = choose([1.0, 1.5, 2.0])
if !is_bat_chase:
dir = choose([Vector2.RIGHT, Vector2.UP, Vector2.LEFT, Vector2.DOWN])
print(dir)

func handle_animation():
var animated_sprite = $AnimatedSprite2D
animated_sprite.play(“fly”)
if dir.x == -1:
animated_sprite.flip_h = true
elif dir.x == 1:
animated_sprite.flip_h = false

func choose(array):
array.shuffle()
return array.front()

this isn’t a chat, you make a post and wait for a response and it can take hours or days. but don’t open a second one, all you do is annoy whoever would’ve helped you, me included.

and you need to format your code with the </> button.

this is wortless code. just assign is_bat_chase when you declare it:


var is_bat_chase: bool = false

use not instead of !. do not use cryptic symbols, this isn’t C#.


if not is_bat_chase:

your script should be on a characterbody2D. make sure it is.

make sure there are no errors in the console. show us if there are.

code is a mess and the work of a bad programmer, but valid. your problem is with the nodes.
make sure _on_timer_timeout() is connected… you copy-pasted this didn’t you?
you need to connect it from the signals section in the inspector, or through code.
and TYPE the code, don’t copy-paste. you learn nothing and also create problems.


you need a timer node, _on_timer_timeout() should have a symbol next to it showing that it’s connected. if it isn’t, add this code to ready:

func ready() -> void:
    $timer.timeout.connect(_on_timer_timeout)

tutorials like this need to be followed exactly or they will break. they are bad tutorials. prefer the ones that explain what each line does and give you a broad base for making your own game. and start small, then make bigger games each time. you can’t make your dream game in the first try. you need to learn first and failing is the only way to do it.

You are calling the move function in _process, but CharacterBody2D uses move_and_slide in _physics_process

func _physics_process(delta: float) -> void:
    move(delta)
    handle_animation()

Make sure to format your code pastes between three ticks ```

To add to the other comments, move_and_slide already uses delta implicitly so depending on what you are trying to achieve you should remove it from your velocity calculation. And are you sure you want to constantly increase the velocity?

Is this your own code or is this the code presented in the tutorial? If the latter I would seriously suggest that you look at another tutorial (e.g. the one in the official documentation).