I assume there is no humanoid, so that you cannot call MoveTo()
I would prefer to use an AlignPosition constraint and have the part CanCollide set to true.
And periodic raycasting down to measure altitude, if you want the part to float. In that case you can add another constraint with constant force upwards, to counter gravity.
If there are obstacles, pathfinding will prevent getting stuck, most of the time.
MoveTo() does go straight, yes. If that is a problem, I guess you need pathfinding. You can use the waypoints any way you like, there is no requirement to use them to move a humanoid. The pathfinding takes positions as argument and returns positions in the waypoints.
LerpTo with the waypoints may be a good first prototype. I am not sure the movement will be natural. The physics constraints may help a little with that. You can also use the AlignOrientation to point the part in the direction of the next waypoint.
function arcaneModule.PathFindScout(model, target)
local destination = target
path:ComputeAsync(model.PrimaryPart.Position, destination.Position)
local Humanoid = model:FindFirstChild("ArcaneHumanoid")
model.Scout.Anchored = false
model.HumanoidRootPart.Anchored = false
local waypoints = path:GetWaypoints()
for i, waypoints in pairs(waypoints) do
Humanoid:MoveTo(waypoints.Position)
Humanoid.MoveToFinished:Wait()
end
end
Explorer:
This is my set up right now, and there no errors, although the model keeps fliping over, and ruins the path, do you know how to fix this?
You’re going to need to use raycasting to prevent the part from going through walls, the short answer is if the ray runs into an object, try to find a new location for it to go to.
Read this if you want a more definitive and descriptive answer
You’re right. Here’s a corrected version of the code:
local humanoid = -- the humanoid you want to move local waypoints = -- a list of waypoints the humanoid should move to local speed = -- the speed at which the humanoid should move local tolerance = -- the distance within which the humanoid is considered to have reached a waypoint
for i, waypoint in ipairs(waypoints) do local raycastParams = RaycastParams.new() raycastParams.FilterType = Enum.RaycastFilterType.Blacklist raycastParams.FilterDescendantsInstances = {humanoid.RootPart}
local raycastResult = workspace:Raycast(humanoid.RootPart.Position, waypoint - humanoid.RootPart.Position, raycastParams)
if raycastResult then
humanoid.MoveTo(raycastResult.Position, nil, true)
else
humanoid:MoveTo(waypoint, nil, true)
end
while true do
if (waypoint - humanoid.RootPart.Position).Magnitude <= tolerance then
break
end
wait()
end
end
This code will error as .MoveTo is not a __namecall and must be called with the object to call the function on as the first argument, no worky pls fix.
local humanoid = -- the humanoid you want to move local waypoints = -- a list of waypoints the humanoid should move to local speed = -- the speed at which the humanoid should move local tolerance = -- the distance within which the humanoid is considered to have reached a waypoint
for i, waypoint in ipairs(waypoints) do local raycastParams = RaycastParams.new() raycastParams.FilterType = Enum.RaycastFilterType.Blacklist raycastParams.FilterDescendantsInstances = {humanoid.RootPart}
local raycastResult = workspace:Raycast(humanoid.RootPart.Position, waypoint - humanoid.RootPart.Position, raycastParams)
if raycastResult then
humanoid.RootPart.CFrame = CFrame.new(raycastResult.Position)
else
humanoid:MoveTo(waypoint, speed)
end
while true do
if (waypoint - humanoid.RootPart.Position).Magnitude <= tolerance then
break
end
wait()
end
end
The variables after humanoid on the first line are commented out, and the code will error if raycastResult is nil. This also doesn’t answer the main question, which was how to prevent the humanoid from going through walls.
This code simply teleports the humanoid through a wall if the raycast collided with an object.
The only useful thing I ever got out of chatgpt is quickly explaining to me new concepts such as “What is an enum” but this holis guy keeps giving huge chunks of code that just gives out the most outrageous code.
This is helpful, but I can’t seem to figure it out completely, if you, (or anyone reading this), know how to code with raycasting, like if you can switch my code, and add the raycasting section, that would be great.
It’s a lot to ask, but I’ve been stuck on this for about a day now, so it would help me a ton!
CODE:
function arcaneModule.PathFindScout(model, target)
local alpha = 0
local speed = 50
local distance = (model.PrimaryPart.Position - target.Position).Magnitude
local relativeSpeed = distance / speed
local startCFrame = model.PrimaryPart.CFrame
local loop = nil
local reachedTarget = Instance.new("BindableEvent")
loop = RunService.Heartbeat:Connect(function(delta)
local goalCFrame = startCFrame:Lerp(target.CFrame, alpha)
model:PivotTo(goalCFrame)
alpha += delta / relativeSpeed
if alpha >= 1 then
loop:Disconnect()
reachedTarget:Fire()
end
end)
reachedTarget.Event:Wait()
end