I need some help with my Code since I can’t find any solution. I’m fairly new to programming especially with GDScript and I’m trying to implement an easy and basic Follow AI Enemy but I can’t find any good Scripts because every other is in Godot 3. In Godot 3 we could use the move_and_slide with arguments likemove_and_slide(direction * speed) but now we can’t and I can’t find a solution, here my Code:
extends CharacterBody2D
const speed = 35
var dir: Vector2
var isBatChase: bool
var player_position
var target_position
@onready var player = get_parent().get_node("CharacterBody2D")
func _physics_process(delta):
player_position = player.position
target_position = (player_position - position).normalized()
func _ready():
isBatChase = true
func _process(delta):
move(delta)
handleAnimation()
func move(delta):
if isBatChase == true:
if position.distance_to(player_position) > 1:
velocity = position.direction_to(player_position) * speed * delta
if isBatChase == false:
velocity += dir * speed * delta
move_and_slide()
func _on_direction_timer_timeout():
$DirectionTimer.wait_time = choose([1.0, 1.5, 2.0])
if isBatChase == false:
dir = choose([Vector2.RIGHT, Vector2.LEFT, Vector2.UP, Vector2.DOWN])
func handleAnimation():
var animatedSprite = $AnimatedSprite2D
animatedSprite.play("flying")
if dir.x == 1:
animatedSprite.flip_h = true
elif dir.x == -1:
animatedSprite.flip_h = false
func choose(array):
array.shuffle()
return array.front()
enter image description here
That is my Scene
Egor Krutsch is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.