I have a custom NPC system that I have made it so that it would follow the target and raycast for hipheight. The problem is that the NPC’s would be laggy even though I have tried using Parallel Luau.
How my system works:
Movement: The NPC would lerp in a loop until it has reached it’s destination.
Example:
repeat
local delta = RunService.Heartbeat:Wait()
elapsedTime += delta
local alpha = math.clamp(elapsedTime / timeToReachTarget, 0, 1)
local lerp = origin:Lerp(position, alpha)
character:MoveTo(lerp)
until elapsedTime >= timeToReachTarget
HipHeight: I raycast down to check if there is a ground under the NPC, but if not, it would continuously until it finds the ground.
Example:
local foundGround, position = GetGroundPosition.GetGroundPosition()
if not foundGround then
origin = HumanoidRootPart.Position - Vector3.new(0, 10, 0)
local distance = (oldPos - origin).Magnitude
local timeToReachTarget = (distance / lerpSpeed)
local alpha = math.clamp(timeToReachTarget, 0, 1)
local lerp = oldPos:Lerp(origin, alpha)
task.synchronize()
character:MoveTo(lerp)
end
Game Link: