New to godot and i’m getting an output I didn’t ask for.
Im making a game, currently I have a short map, a player and an enemy. When running the script the enemy should play its idle animation but instead it plays its jump animation. I’m not getting any error messages either
extends CharacterBody2D
var speed = 50
#gravity
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
var player
var chase = false
func _on_ready():
get_node("AnimatedSprite2D").play("Idle")
func _physics_process(delta):
# Add the gravity.
velocity.y += gravity * delta
move_and_slide()
#when exiting the right or left side of player detection collision shape stop movement
func _on_playerdetection_right_body_exited(body):
chase = false
player = get_node("../../Player/Player")
get_node("AnimatedSprite2D").play("Idle")
if body.name == "Player":
velocity.x = 0
func _on_playerdetection_left_body_exited(body):
chase = false
player = get_node("../../Player/Player")
get_node("AnimatedSprite2D").play("Idle")
if body.name == "Player":
velocity.x = 0
#player detection - when entering left side of enemys collision flip horisontally and chase the
#player.
func _on_playerdetection_left_body_entered(body):
print("left")
get_node("AnimatedSprite2D").play("Jump")
player = get_node("../../Player/Player")
if body.name == "Player":
var direction = (player.position - global_position).normalized()
if direction.x > 0:
chase = true
get_node("AnimatedSprite2D").flip_h = true
velocity.x = direction.x * 50
else:
velocity.x = 0
func _on_playerdetection_right_body_entered(body):
print("right")
get_node("AnimatedSprite2D").play("Jump")
player = get_node("../../Player/Player")
if body.name == "Player":
var direction = (player.position - global_position).normalized()
if direction.x > 0:
chase = true
get_node("AnimatedSprite2D").flip_h = true
velocity.x = direction.x * 50
else:
velocity.x = 0
the print() functions are for me to see if player collision is working or not. When running the script I get
“right”
“left”
outputted, don’t know why
I want it to play the idle animation when running the script
extends CharacterBody2D
var speed = 50
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
var chase = false
@onready var animation = #drag your animation node here
@onready var player = #drag your player node here
@onready var sprite = #drag your enemy's sprite node here
#Then create one Area2d, which will cover area you want your enemy see player
func _on_Area2d_entered(body):
chase = true
func _on_Area2d_exited(body):
chase = false
#Then check if it's chasing player
func _physics_process(delta):
velocity.y += gravity * delta
move_and_slide()
if chase == true:
position += (player.position - position)/speed
animation.play("Jump")
if(player.position - position) < 0:
sprite.flip_h = true
else:
sprite.flip_h = false
else:
animation.play("Idle")
It will optimize your code a little and might be solution for your problem
extends CharacterBody2D
var speed = 50
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
var chase = false
#drag your animation node here
@onready var animation = get_node("AnimatedSprite2D")
#drag your player node here
@onready var player = get_node("../../Player/Player")
#drag your enemy's sprite node here
@onready var sprite = get_node(".")
#Then create one Area2d, which will cover area you want your enemy see player
func _on_playerdetection_body_entered(_body):
chase = true
func _on_playerdetection_body_exited(_body):
chase = false
#Then check if it's chasing player
func _physics_process(delta):
velocity.y += gravity * delta
move_and_slide()
if chase == true:
position += (player.position - position)/speed
animation.play("Jump")
if(player.position - position) > 0:
$AnimatedSprite2D.flip_h = true
else:
$AnimatedSprite2D.flip_h = false
else:
animation.play("Idle")
Made it like this. When running the script I get an error message for
if(player.position - position) < 0:
I get the error message" invalids operand ‘vector2’ and ‘int’ in operator ‘<’."
might be because I miswrote something
Alright some progress. movement seems to be working although now it chases the player regardless of whether or not the player is inside the collision area. This seems to only be an issue when running the script in the start. it fixes itself after manually entering and exiting the collision area
hahah oh yeah i added that after. no worries also added
var direction = (player.position - global_position).normalized()
for movement but now when running the script the enemy chases the player before entering the collision shape. Might be because I messed up somewhere along the lines
The new script looks like this
extends CharacterBody2D
var speed = 50
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
var chase = false
#drag your animation node here
@onready var animation = get_node("AnimatedSprite2D")
#drag your player node here
@onready var player = get_node("../../Player/Player")
#drag your enemy's sprite node here
@onready var sprite = get_node(".")
#Then create one Area2d, which will cover area you want your enemy see player
func _on_playerdetection_body_entered(_body):
chase = true
func _on_playerdetection_body_exited(_body):
chase = false
#Then check if it's chasing player
func _physics_process(delta):
velocity.y += gravity * delta
move_and_slide()
if chase == true:
var direction = (player.position - global_position).normalized()
velocity.x = direction.x * speed
if direction.length() < 0:
get_node("AnimatedSprite2D").flip_h = true
else:
get_node("AnimatedSprite2D").flip_h = false
else:
animation.play("Idle")
velocity.x = 0