Hi!
I’m having an issue with using vectorForce on mobs and them glitching out. What i’m trying to accomplish it moving the brick (which is the mob) towards the player using flowfields and it works fine sometimes, but other times the parts glitch out and I don’t really know why. How the algorithm works is that the mob is moving towards the closest neighbor arrow until it reaches the player.
Video below:
The algorithm visualized works like the video below, where the black bricks is the player:
Code where I’m moving the mobs:
function enemy:UpdatePosition(flowfield, index)
--if mob does not exist, remove
if (self.hitBox == nil) then
self:Remove(index)
return
end
--rounding to floor
local floor = {
X = roundToPosition(self.hitBox.Position.X),
Z = roundToPosition(self.hitBox.Position.Z)
}
--If mob is outside the map, then remove
if(floor.X > arrayDimension or floor.X < 1) or (floor.Z > arrayDimension or floor.Z < 1)then
self:Remove(index)
return
end
--if mob hit a wall corner, then use the old value
if((flowfield[(((floor.X)-1)*arrayDimension)+floor.Z]) and ((flowfield[(((floor.X)-1)*arrayDimension)+floor.Z]) ~= Vector3.new(0, 0, 0)))then
f21 = flowfield[(((floor.X)-1)*arrayDimension)+floor.Z]
self.lastPosition = f21
else
f21 = self.lastPosition
end
--Force part
local direction2 = f21
local wantedVelocity = direction2 * self.maxSpeed
local velocityChange = wantedVelocity - self.hitBox.Velocity
self.hitBox.VectorForce.Force = velocityChange * (self.maxForce / self.maxSpeed)*100
--Turning the brick towards the destination
local zombieOrientation = CFrame.lookAt(self.hitBox.Position, self.hitBox.Position + direction2)
self.hitBox:FindFirstChild("BodyGyro").CFrame = zombieOrientation
return self
end
I’m a little bit stuck here and any help would be appreciated