As the title says, trying to make my first game on here and have the enemy pick a random coordinate to patrol to when the player is outside its detection radius. It seems to be picking a coordinate and then not refreshing because it just floats to the bottom left corner whenever the player is too far away. Any help appreciated!
go.property("dir", vmath.vector3())
local reg_speed = 30
local radius = 100
function init(self)
self.dir = vmath.vector3()
self.speed = reg_speed
end
function update(self, dt)
local p = go.get_position()
local player_pos = go.get_position("Player")
local direction = player_pos - p
if vmath.length(player_pos - p) <= radius then
if vmath.length_sqr(direction) > 0 then
direction = vmath.normalize(direction)
end
go.set_position(p + direction * self.speed * dt)
else
local x = math.random(0,15)
local y = math.random(0,15)
local target = vmath.vector3(x,y,1)
local distance = target - p
if vmath.length_sqr(distance) > 0 then
distance = vmath.normalize(distance)
end
go.set_position(p + distance * self.speed * dt)
end
end