Godot Version
3.6
Question
Hello everyone! In my 2d game, there is a level with 200+ mobs. Each of them is a KinematicBody2d. Upon entering the level, it starts to lag a lot. And I want to know how to optimize it? I will be glad of any help.
this is the code that is used for mobs:
extends KinematicBody2D
var velocity = Vector2(50, 0)
var gravity = 15
var turned_side
var health = 100
func _physics_process(_delta):
velocity.y += gravity
move_and_slide(velocity,Vector2.UP)
func change_state():
velocity.x *= -1
scale.x *= -1
if velocity.x < 0:
turned_side = true
elif velocity.x > 0:
turned_side = false
onready var _animated_sprite = $AnimatedSprite
func _on_detech_player_body_entered(body):
if body.is_in_group('player'):
_animated_sprite.play("attack")
velocity = Vector2(0,0)
func _on_detech_player_body_exited(body):
if body.is_in_group('player'):
_animated_sprite.play("walk")
if turned_side == true:
velocity.x = -50
elif turned_side == false:
velocity.x = 50
func _on_attack_to_player_body_entered(body):
if body.is_in_group('player'):
body.attack_detech()
func _on_player_is_back_body_entered(body):
if body.is_in_group('player'):
change_state()
func attacked(damage):
$TextureProgress.visible = true
health -= damage
$TextureProgress.value = health
$Timer.wait_time = 1
$Timer.start()
if health <= 0:
_animated_sprite.play("death")
yield($AnimatedSprite, "animation_finished")
queue_free()
func _on_Timer_timeout():
$TextureProgress.visible = false```