`extends CharacterBody2D
var chase = false
var speed = 100
@onready var anim = $AnimatedSprite2D
func _physics_process(delta: float) → void:
# Add the gravity.
if not is_on_floor():
velocity += get_gravity() * delta
var player = $"../../player/player1"
var direction = (player.position - self.position).normalized()
if chase == true:
velocity.x =direction.x * speed
anim.play("run")
else:
velocity.x = 0
anim.play("Idol")
if direction < 0:
$AnimatedSprite2D.flip_h = true
else :
$AnimatedSprite2D.flip_h = false
move_and_slide()
func _on_detector_body_entered(body: Node2D) → void:
if body.name == “player1”:
chase = true
func _on_detector_body_exited(body: Node2D) → void:
if body.name == “player1”:
chase = false
`
помогите я написал скрипт а игра вылетает , а точнее из-за этой части` if direction < 0:
$AnimatedSprite2D.flip_h = true
else :
$AnimatedSprite2D.flip_h = false`
What is the error you are getting? Please be more specific.
1 Like
I created the mob behavior and when I was almost done there was only a part where it could turn but when I wrote that part when I started the game it started to crash the code itself and the part that was causing the crash are written in the question
When the game crashes, what line does it give you in the error code, and what is the error? At the bottom of the editor when the game crashes it should give you an error message in red.
The game crashes when there are these 4 lines in the code
if direction < 0: $AnimatedSprite2D.flip_h = true else : $AnimatedSprite2D.flip_h = false
after the game crashes in console this error appears
invalid operands vector 2" and "int" in operator ">"
the error is that the game does not work because of the lines in the code that are responsible for turning the character sprite
That isn’t the error. The error is that direction is a Vector2 and you are checking if it is smaller than an Int.
Try if direction.x < 0:
Thank you, everything worked, I thought about the solution for a long time and you helped me, I hope you will help me again someday:))))
1 Like