I am busy trying to learn how to code top-down rpgs and when I tried to implement a navmesh so my enemies don't run straight into walls, I ran into a problem that I coudln't fix by searching online, at least so far. This is the current enemy script, and if you need any extra details I can provide them. The player is declared in the player script as the player variable for the enemy script.
I am not sure how to link my project.
I am really sorry, I am really new to godot and have to pull from several tutorials to get it to work.
The enemy doesn't move at all really.
I have the tilemaps, player, enemy and navmesh ysorted.
the tree hierarchy looks like this:
world-SPATIAL2D
ysort
navigation2d
nav-polygon
tilemap
enemy
tilemap2(bushes and rocks)
tilemaptree(trees)
player
camera
extends KinematicBody2D
const MOVE_SPEED = 400
const DETECT_RANGE = 500
const ATTACK_DIST = 100
onready var sprite = $Sprite
onready var anim = $AnimationPlayer
onready var nav = get_parent()
onready var raycast = $RayCast2D
var path = []
var path_ind = 0
var player = null
func _ready():
add_to_group("ninja")
func move_to(target_pos):
path = nav.get_simple_path(global_transform.origin, target_pos)
path_ind = 0
func _physics_process(delta):
if player == null:
return
if path_ind < path.size() :
var move_vec = path[path_ind] - global_position
if move_vec.length() < 0.1:
path_ind += 1
else:
move_and_slide(move_vec * MOVE_SPEED)
var target = raycast.look_at(player.global_position)
if player.global_position - global_position <= Vector2(DETECT_RANGE, DETECT_RANGE):
move_to(player.global_position - global_position)
func set_player(p):
player = p