Hey, I made a entity moving system with lerp, and I need to make them to look in the way they’re walking, but I don’t really understand how to make this…
task.spawn(function()
while true do
local dt = task.wait()
for index, newEntity in ipairs(Entities) do
local distance = (newEntity.CurrentPoint.Position - newEntity.Cframe.Position).Magnitude
local speed = newEntity.Speed*dt
local estimatedTime = speed / distance
local adjustedLerpAlpha = math.min(estimatedTime,1)
newEntity.Cframe = newEntity.Cframe:Lerp(newEntity.CurrentPoint.CFrame , adjustedLerpAlpha) -- store the new cframe
workspace.Enemy:FindFirstChild(newEntity.EntityId).Value = newEntity.Cframe -- update cframe value
newEntity["Model"].CFrame = newEntity.Cframe -- update part positon
if adjustedLerpAlpha >= 1 then
if newEntity.Progress >= #Path:GetChildren()-1 then
newEntity["Model"]:Destroy()
ClientReplicator:FireAllClients("Destroy", newEntity.EntityId)
newEntity = nil
else
newEntity.Progress += 1
part2 = workspace.Map.Waypoints:FindFirstChild(tostring(newEntity.Progress))
newEntity.CurrentPoint = part2
end
end
end
end
end)
Can someone tell me how to make this rotation in my case?
After some research and help of gpt I got this result, but this with bugs. How can I do the smooth turning in my case?
task.spawn(function()
while true do
local dt = task.wait()
for index, newEntity in ipairs(Entities) do
local distance = (newEntity.CurrentPoint.Position - newEntity.Cframe.Position).Magnitude
local speed = newEntity.Speed * dt
local estimatedTime = speed / distance
local adjustedLerpAlpha = math.min(estimatedTime, 1)
local newCFrame = newEntity.Cframe:Lerp(newEntity.CurrentPoint.CFrame, adjustedLerpAlpha)
-- Calculate the direction vector
local direction = (newEntity.CurrentPoint.Position - newEntity.Cframe.Position).unit
-- Create a new CFrame with the calculated direction
local lookAtCFrame = CFrame.new(newCFrame.Position, newCFrame.Position + direction)
-- Update entity's CFrame
newEntity.Cframe = lookAtCFrame
-- Update entity's position
workspace.Enemy:FindFirstChild(newEntity.EntityId).Value = newEntity.Cframe
-- Update entity's part position
newEntity["Model"].CFrame = newEntity.Cframe
if adjustedLerpAlpha >= 1 then
if newEntity.Progress >= #Path:GetChildren() - 1 then
newEntity["Model"]:Destroy()
ClientReplicator:FireAllClients("Destroy", newEntity.EntityId)
newEntity = nil
else
newEntity.Progress += 1
local part2 = workspace.Map.Waypoints:FindFirstChild(tostring(newEntity.Progress))
newEntity.CurrentPoint = part2
end
end
end
end
end)
And why this bug happen? It appear only on the first corner
I tried right now to align parts on server side, but still something going wrong… I read this post on forum and made from this example: An easier way to use AlignOrientation
Is this a good one? Because I have set the align orientation inside part as intended but orientation didn’t happen…
local function findAngle(positionA, positionB)
local CF=CFrame.new(positionA, positionB)
local targetX, targetY, targetZ=CF:ToEulerAnglesXYZ()
local finalizedAngle=CFrame.Angles(targetX, targetY, targetZ)
return finalizedAngle
end
task.spawn(function()
while true do
local dt = task.wait()
for index, newEntity in ipairs(Entities) do
local distance = (newEntity.CurrentPoint.Position - newEntity.Cframe.Position).Magnitude
local speed = newEntity.Speed * dt
local estimatedTime = speed / distance
local adjustedLerpAlpha = math.min(estimatedTime, 1)
local newCFrame = newEntity.Cframe:Lerp(newEntity.CurrentPoint.CFrame, adjustedLerpAlpha)
newEntity.Cframe = newCFrame
-- Update entity's position
workspace.Enemy:FindFirstChild(newEntity.EntityId).Value = newEntity.Cframe
-- Update entity's part position
newEntity["Model"].CFrame = newEntity.Cframe
newEntity["Model"].AlignOrientation.CFrame = findAngle(newEntity.Cframe.Position, newEntity.CurrentPoint.Position)
if adjustedLerpAlpha >= 1 then
if newEntity.Progress >= #Path:GetChildren() - 1 then
newEntity["Model"]:Destroy()
ClientReplicator:FireAllClients("Destroy", newEntity.EntityId)
newEntity = nil
else
newEntity.Progress += 1
local part2 = workspace.Map.Waypoints:FindFirstChild(tostring(newEntity.Progress))
newEntity.CurrentPoint = part2
newEntity["Model"].AlignOrientation.Attachment0 = newEntity.CurrentPoint:FindFirstChild("Attachment")
end
end
end
end
end)
I got something like this, every time the target point is changed I also change the attachment of align orientation… But probably the function with calculations are not the good one for me. I also tried the CFrame.lookAt()