Hello.
Im currently making a AI where the characters “Waist” Motor6D will rotate to the targetpart positions. The code is very simple.
wait()
local waist = script.Parent.UpperTorso:WaitForChild("Waist")
local origin = waist.C0
while game["Run Service"].Heartbeat:Wait() do
waist.C0 = CFrame.new(waist.C0.Position,waist.C0.Position+(game.Workspace.Target.Position - waist.C0.Position).Unit)
end
the idea is that the script would make the waist c0 turn to a rotation by setting the default waistc0 positions and then target the rotation on the direction of the targetparts.
However the results were like this
It just rotates on a weird rotation that has nothing to do with its direction. I seem to be stucking on this for a while now. I tried to find solutions but It doesnt seem to work.
function findNearestTorso(pos)
local list = game.Workspace:children()
local torso = nil
local dist = math.huge
local temp = nil
local human = nil
local temp2 = nil
for x = 1, #list do
temp2 = list[x]
if (temp2.className == "Model") and (temp2 ~= script.Parent) then
temp = temp2:findFirstChild("HumanoidRootPart")
human = temp2:findFirstChild("Humanoid")
if (temp ~= nil) and (human ~= nil) and (human.Health > 0) then
if (temp.Position - pos).magnitude < dist then
torso = temp
dist = (temp.Position - pos).magnitude
end
end
end
end
return torso
end
while true do
wait(0)
local target = findNearestTorso(script.Parent.HumanoidRootPart.Position)
if target ~= nil then
script.Parent.CFrame = CFrame.new(script.Parent.Position, target.Position)
end
end
wait()
local waist = script.Parent.UpperTorso:WaitForChild("Waist")
local origin = waist.C0
while game["Run Service"].Heartbeat:Wait() do
waist.C0 = CFrame.new(waist.C0.Position, game.Workspace.Target.Position-script.Parent.UpperTorso.Position-waist.C0.Position)
end