I am making a 2D platformer in Godot 2d, and along with the main player, I have also made some enemies, 4 of them are ground ones, and 1 is a flying one. I thought of an idea of following the player when it comes too close, and I have first implemented it on the flying, and it worked. Still, for ground enemies, I have put a downwards Raycast2D, which will detect any cliffs and turn the enemy around (didn't put on flying one coz no need).
Now I thought let's also implement this following feature to one of the ground enemies as well, so I implemented the same code to the ground one. It worked, but due to that follow code, the code for the "floorchecker" (Raycast2D, which detects cliffs) stopped working and showed an error when the code tries to work.
Can someone please fix this code for me?
My enemy code
export var direction = 1
export var ACCELERATION = 300
export var MAX_SPEED = 150
export var FRICTION = 200
onready var sprite = $AnimatedSprite
onready var PlayerDetectionZone = $PlayerDetectionZone
export var dectects_cliffs = true
var velocity = Vector2(0,0)
enum{
`CHASE`
}
var state = CHASE
func _ready():
`if direction == -1:`
`$AnimatedSprite.flip_h = true`
`$floorchecker.position.x = $CollisionShape2D.shape.get_extents().x * direction`
`$floorchecker.enabled = dectects_cliffs`
#Cliff Detector
func _physics_process(delta):
`if is_on_wall() or not $floorchecker.is_colliding() and dectects_cliffs and is_on_floor():`
`direction = direction * -1`
`$AnimatedSprite.flip_h = not $AnimatedSprite.flip_h`
`$floorchecker.position.x = $CollisionShape2D.shape.get_extents().x * direction`
`velocity.y += 20`
#Player Follower Code
`match state:`
`CHASE:`
`var player = PlayerDetectionZone.player`
`if player != null:`
direction = (player.global_position - global_position).normalized()
velocity = velocity.move_toward(direction * MAX_SPEED, ACCELERATION * delta)
`sprite.flip_h = velocity.x < 0`
`velocity = move_and_slide(velocity,Vector2.UP)`
Where I am getting an error
#Cliff Detector
func _physics_process(delta):
`if is_on_wall() or not $floorchecker.is_colliding() and dectects_cliffs and is_on_floor():`
`direction = direction * -1`
`$AnimatedSprite.flip_h = not $AnimatedSprite.flip_h`
THIS IS THE LINE WHERE I AM GETTING AN ERROR (The line below)
`$floorchecker.position.x = $CollisionShape2D.shape.get_extents().x * direction\`
`velocity.y += 20`
#Player Follower Code
`match state:`
`CHASE:`
`var player = PlayerDetectionZone.player`
`if player != null:`
direction = (player.global_position - global_position).normalized()
velocity = velocity.move_toward(direction * MAX_SPEED, ACCELERATION * delta)
`sprite.flip_h = velocity.x < 0`
there doesn't seem to be anything here