Okay. Basically I've been working on a CFrame based walking system for NPCS. Now, You may be asking why CFrame based? well here's 3 reasons why.
Reason 1.
The risk of an NPC glitching out or getting stuck is literally null since, You don't aren't relying on physics to move your NPC
Reason 2.
You don't have to worry about NetworkOwnership and exploiters being able to mess up the NPC. Unless there's a backdoor in your game, I think you should check your game for backdoors. I recommend searching for scripts named "Fix", "Weld" or "Anticheat"
Reason 3.
You can make your NPC go as fast as you want. No overshooting, Jittering, etc.
The issue:
I'm trying to make it so that the NPC faces in the direction it is walking. When I put 0 as the Y value of the direction Vector3 it teleports into interstellar space (literally). If I don't do that, It looks at the ground.
The code that's being annoying:
local function restockerGoTo(pos)
local path = pathfindingService:CreatePath({
AgentRadius = 5;
Costs = {
DoorMass = math.huge
}
})
path:ComputeAsync(restockerNPCHum.RootPart.Position, pos)
for i, v in ipairs(path:GetWaypoints()) do
local direction = (restockerNPCHum.RootPart.Position - v.Position)
direction = Vector3.new(direction.X, 0, direction.Z)
local calculatedTime = (direction.Magnitude/restockerNPCHum.WalkSpeed)
local goalPosition = v.Position + Vector3.new(0, restockerNPCHum.HipHeight, 0)
local calculatedCFrame = CFrame.new(goalPosition, restockerNPCHum.RootPart.Position - direction)
local tween = tweenService:Create(restockerNPCHum.RootPart, TweenInfo.new(calculatedTime, Enum.EasingStyle.Linear), {CFrame = calculatedCFrame})
tween:Play()
tween.Completed:Wait()
end
end
> Yes, I went through the trouble of making the post look nice with HTML.
Use CFrame.LookAt() constructor using the current position of the NPC and the lookat being the next position then squash the Y value of the next position.
local charPos = char:GetPivot().Position
local lookAt = goal.Position * Vector3.new(1,0,1)
char:PivotTo(CFrame.LookAt(charPos, lookAt)) --This will turn the char to face the goal position
It’s actually working!
Well, Kinda. It’s just looking towards the goal but the tweening is messing it up… I gotta go to bed, I’ll figure this out tomorrow. GN.
I have no idea why the tool is affecting it. It really shouldn’t. Honestly now that I think about it, there really no good reason why you can’t tween the position and rotation at the same time. It will just turn as it moves
local function restockerGoTo(pos)
local path = pathfindingService:CreatePath({
AgentRadius = 5;
Costs = {
DoorMass = math.huge
}
})
path:ComputeAsync(restockerNPCHum.RootPart.Position, pos)
for i, v in ipairs(path:GetWaypoints()) do
local direction = (restockerNPCHum.RootPart.Position - v.Position)
direction = Vector3.new(direction.X, 0, direction.Z)
local calculatedTime = (direction.Magnitude/restockerNPCHum.WalkSpeed)
local goalPosition = v.Position + Vector3.new(0, restockerNPCSize.Y/2, 0)
local calculatedCFrame = CFrame.new(goalPosition)
local tween = tweenService:Create(restockerNPCHum.RootPart, TweenInfo.new(calculatedTime, Enum.EasingStyle.Linear), {CFrame = calculatedCFrame})
tween:Play()
tween.Completed:Wait()
local charPos = restockerNPC:GetPivot().Position
local lookAt = goalPosition * Vector3.new(1,1,1)
local lookTowards = CFrame.lookAt(charPos, lookAt)
restockerNPC:PivotTo(lookTowards)
end
end