Hello,
I am not really sure how to describe this "problem" properly. I am currently coding a enemy that can walk on wall's, the floor and the ceiling. When i set the direction value to 1.0 i need less code to make the enemy move without any movement breaking bugs. But when i set the direction value to -1.0 I suddenly need to calculate the movement in the ceiling logic twice. If I don't do this the enemy will get stuck after it arrives the third time at the top-left corner.
I first worked on the positive direction logic so I thought I just need to adjust it to the negative direction but somewhere something is wrong and I don't understand why.
extends KinematicBody2D
export var speed := 250.0
export var direction := -1.0
var _velocity := Vector2.ZERO
var _up_direction := Vector2.UP
onready var back: RayCast2D = $Back
onready var front: RayCast2D = $Front
onready var left: RayCast2D = $Left
onready var right: RayCast2D = $Right
func _physics_process(delta: float) -> void:
if direction > 0.0: #positive direction
if back.is_colliding() and front.is_colliding():
#floor logic
if is_on_floor() and right.is_colliding():
rotate(deg2rad(-90))
elif is_on_floor():
_velocity = Vector2(speed * direction, 0.0)
#wall logic
if is_on_wall() and right.is_colliding():
rotate(deg2rad(-90))
elif is_on_wall() and back.get_collision_normal() == Vector2.RIGHT:
_velocity = Vector2(0.0, speed * direction)
elif is_on_wall() and back.get_collision_normal() == Vector2.LEFT:
_velocity = Vector2(0.0, speed * -direction)
#ceiling logic
if is_on_ceiling() and right.is_colliding():
rotate(deg2rad(-90))
elif is_on_ceiling():
_velocity = Vector2(speed * -direction, 0.0)
else: #negative direction
if back.is_colliding() and front.is_colliding():
#floor logic
if is_on_floor() and left.is_colliding():
if left.get_collision_normal() == Vector2.UP:
rotate(deg2rad(90))
elif is_on_floor():
_velocity = Vector2(speed * direction, 0.0)
#wall logic
if is_on_wall() and left.is_colliding():
rotate(deg2rad(90))
elif is_on_wall() and front.get_collision_normal() == Vector2.RIGHT:
_velocity = Vector2(0.0, speed * direction)
elif is_on_wall() and front.get_collision_normal() == Vector2.LEFT:
_velocity = Vector2(0.0, speed * -direction)
#ceiling logic
if is_on_ceiling() and left.is_colliding():
rotate(deg2rad(90))
#on negative direction the ceiling logic needs to have the _velocity calculation
#in 2 places and I have no clue why (look positive direction ceiling logic)
# _velocity = Vector2(speed * -direction, 0.0) #1
elif is_on_ceiling():
_velocity = Vector2(speed * -direction, 0.0)#2
_velocity = move_and_slide(_velocity, Vector2.UP)
This is how my enemy scene is setup
