I have some trouble with my enemy movement script, it keeps spawning the enemy at this specific spot on the map
and I don’t know how to fix it.
local Tween = game:GetService("TweenService")
local enemy = workspace.Model
local Path = workspace.Map.Movement
local Spawn = workspace.Map.Movement.Spawn
local speed = 3
local rotspeed = 1
local function getCirclePoint(angle, radius, center)
local x = center.X + radius * math.cos(angle)
local y = center.Y + radius * math.sin(angle)
return Vector2.new(x, y)
end
local function getTweenPoints(corner, radius)
local points = {}
for i=0,360,15 do
local point = getCirclePoint(math.rad(i), radius, corner)
table.insert(points, point)
end
return points
end
local function getRotation(startPos, endPos)
local deltaX = endPos.X - startPos.X
local deltaY = endPos.Y - startPos.Y
local angle = math.atan2(deltaY, deltaX)
return angle
end
function MoveEnemy(entity)
for i = 1, #Path:GetChildren() do
local waypoint = Path[i]
if waypoint.Name == "Corner" then
local tweenPoints = getTweenPoints(waypoint.Position, 2)
for i=1,#tweenPoints-1 do
local Walking = Tween:Create(entity.HumanoidRootPart, TweenInfo.new((tweenPoints[i] - tweenPoints[i+1]).Magnitude/speed, Enum.EasingStyle.Linear, Enum.EasingDirection.In), {CFrame = CFrame.new(tweenPoints[i])})
Walking:Play()
Walking.Completed:Wait()
end
else
local targetPos = waypoint.Position
local currentPos = entity.HumanoidRootPart.Position
local rotation = getRotation(currentPos, targetPos)
local Rotating = Tween:Create(entity.HumanoidRootPart, TweenInfo.new(rotspeed, Enum.EasingStyle.Linear, Enum.EasingDirection.In), {CFrame = CFrame.fromEulerAnglesXYZ(0, rotation, 0)})
Rotating:Play()
Rotating.Completed:Wait()
local Walking = Tween:Create(entity.HumanoidRootPart, TweenInfo.new((currentPos - targetPos).Magnitude/speed, Enum.EasingStyle.Linear,Enum.EasingDirection.In), {CFrame = waypoint.CFrame})
Walking:Play()
Walking.Completed:Wait()
end
end
end
while true do
local entity = enemy:Clone()
entity.HumanoidRootPart.CFrame = Spawn.CFrame
entity.Parent = workspace.Enemies
MoveEnemy(entity)
wait(2)
end
This part is where the enemy is suppose to spawn