I want the pets to walk to points around an enemy and attack it. however it sometimes works and sometimes gets glitchy and teleports everywhere kinda. Is it something to do with collisions or the way im doing it isnt right? code and vid are given below:
while true do
if script.Parent.isAttacking.Value == true then
local pet = script.Parent
local targetInstance = script.Parent.target.Value
local rayOrigin = targetInstance.PrimaryPart.Position
local rayDirection = targetInstance.PrimaryPart.CFrame.UpVector
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {targetInstance, pet}
local ray = game.Workspace:Raycast(rayOrigin, -rayDirection * 10, raycastParams)
local petHeight = pet.Hitbox.Size.Y
local chosenAttachment = targetInstance.PrimaryPart:FindFirstChild(script.Parent.attackAttName.Value)
if ray then
local lookPos = Vector3.new(targetInstance.PrimaryPart.Position.X, pet.Hitbox.Position.Y,targetInstance.PrimaryPart.Position.Z)
local dist = (ray.Position - rayOrigin).Magnitude
local yDiff = dist - (petHeight/2)
for i = 1, 100, 1 do
pet.PrimaryPart.CFrame = pet.PrimaryPart.CFrame:Lerp(CFrame.new(chosenAttachment.WorldPosition - Vector3.new(0,yDiff,0)), i/100)
pet.PrimaryPart.CFrame = CFrame.new(pet.PrimaryPart.Position, lookPos)
wait(0.02)
end
else
for i = 1, 100, 1 do
pet.PrimaryPart.CFrame = pet.PrimaryPart.CFrame:Lerp(CFrame.new(targetInstance.PrimaryPart.Position - Vector3.new(3,0,3)), i/100)
wait(0.02)
end
end
end
task.wait()
end