@onready var planta = get_tree().get_first_node_in_group(“planta”) @onready var sprite = $Sprite2D
func _physics_process(_delta):
var direction = global_position.direction_to(planta.global_position)
velocity = direction*movement_speed
move_and_slide()
Hello, I wrote the code and all these errors appear
Invalid character ““” (U+201C).
Invalid character “”” (U+201D).
(STANDALONE_EXPRESSION):Standalone expression (the line has no effect)
Now it’s fixed a little, it just keeps giving the error
(STANDALONE_EXPRESSION):Standalone expression (the line has no effect)
It says it is on line 20 and on line 20 is the move code address. I don’t know if that’s why.
I’ll leave you the code again just in case
@export var movement_speed = 40.0
@onready var planta = get_tree().get_first_node_in_group(“planta”) @onready var sprite = $Sprite2D
func _physics_process(_delta):
var direction = global_position.direction_to(planta.global_position)
velocity = direction*movement_speed
move_and_slide()
if direction.x > 0:
sprite.flip_h = false
elif direction.x < 0:
sprite.flip_h = true
if is_instance_valid("planta"):
direction
else:
planta = get_tree().get_first_node_in_group("planta")
func _ready():
$lil.play(“move”)
func _on_area_2d_area_entered(_area):
if _area.is_in_group(“planta_area”):
$lil.play(“attack”)
direction alone doesn’t do anything, hence the error.
It should look something like this (tabs/spaces are probably wrong because I copied your unformated code):
func _physics_process(_delta):
# Run movement code if planta exists
if is_instance_valid(planta):
var direction = global_position.direction_to(planta.global_position)
velocity = direction*movement_speed
if direction.x > 0:
sprite.flip_h = false
elif direction.x < 0:
sprite.flip_h = true
move_and_slide()
# Find new planta if it doesn't exist anymore
else:
get_tree().get_first_node_in_group(“planta”)
The game no longer crashes when the enemy destroys the plant but the enemy doesn’t move so I put a move_and_slide on the else but the enemy doesn’t go towards the plant until it looks like it’s going to the opposite side.
That’s because you didn’t find a new plant and you didn’t update the velocity of your character. When you call move_and_slide() it will use the velocity you last set. And in your case it was the direction to your last plant.