[UNSOLVED] LerpTo not go through walls

Is there a way to make a part move using: LerpTo be able to not go through walls?

I have a part that goes around random waypoints through out the map (using LerpTo), but I want the part to not go through any walls/obstacles.

Is there a way to accomplish this, or would I have to discard my script and use a pathfinding script instead?

I’m just looking for a script example, and not a whole entire script of the movement, etc.

Any help is appreciated!
(i know this is a short post, so just ask if you need more information)

1 Like

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.

2 Likes

I could add a humanoid, but I thought MoveTo() just goes in a straight direction.

I never used AlignPosition, but I have used pathfinding in an npc, not a single part.

Any ways you think is the best way to accomplish what I’m trying to do?

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.

1 Like
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:
explorer screen shot

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?

MoveTo doesn’t exist on parts, and Position doesn’t exist on humanoids muchacho

Try adding vector3.new(0,3,0) in the MoveTo call, just to confirm it is not the height causing problems.

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

Please. Don’t give this guy any solutions. @uwuCulturist just exposed how it works :sob:

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.

Sure, here’s the fixed 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.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.

1 Like

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

I don’t know what else to say but… this doesn’t work in the slightest.

2 Likes