@fire7side
Hi, it's me again. So I was working on an alternative solution that is incredibly simple, noob friendly and only required one ray cast( So it's less taxing on the hardware). Here is what came up with:
extends KinematicBody
onready var raycast = $RayCast;onready var eyes = $Eyes
onready var anim_player = $AnimationPlayer; var target
export var sweep = 0;var sweep_range = 90; var ray_length = -5
var health = 100;var speed = 5;var turning_speed = 10
var direction = Vector3();var fall = Vector3()
var gravity = 9.8
func _ready():
yield(owner,"ready")
target = owner.point
func _process(delta):
if health < 50:queue_free()
patrolling()
if not is_on_floor():fall.y -= gravity * delta #Code for gravity.
func move_foward(speed = 7):
direction -= transform.basis.z;direction = direction.normalized()
move_and_slide(direction * speed, Vector3.UP,true)
func sweeping():
sweep = wrapi(sweep + 30,-sweep_range,sweep_range)
raycast.rotation_degrees.y = sweep
func patrolling():
move_foward(speed)
if not raycast.is_colliding():sweeping()
else:avoid_obstacle()
move_and_slide(fall, Vector3.UP)
func avoid_obstacle():
if raycast.rotation_degrees.y < 0:rotate_y(deg2rad(turning_speed))
elif raycast.rotation_degrees.y >= 0:rotate_y(deg2rad(-turning_speed))
Now here is what it looks like in action:
Now the biggest problem with my approach is that I haven't figured out how to make my "AI" chase down a target while avoiding obstacles. If I don't find a way to make the enemy avoid obstacles while chasing down a target, I might have to scrap this idea. I've just started brainstorming on how to solve this problem but, I also want to ask what do you think. Do you know of a simpler solution on how to get the enemies to avoid obstacles while chasing down a target?