Move parallel to a wall

I am trying to make a mob move parallel to a wall if it is approaching one. I calculate a target position for the mob, then use raycast to determine if and where the contact occurs within a certain distance. If it does, then the target position should be changed to be parallel to the wall & in the same orientation of the raycast. What I’m looking for:

My problem is determining how to set the CFrame of the mob so it runs parallel to that wall. I’m thinking I could use the orientation of the wall + or - 90 degrees to get the parallel orientation, but wasn’t sure how to use that to change the mob’s CFrame. So far I have:

local targetPos = --code calculating the target Vector (solid line in picture)

-- See if wall in the way
local walls = workspace.Walls:GetChildren()
local closeDistance = 5
		
local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Include
raycastParams.FilterDescendantsInstances = {workspace.Walls}
local raycastResult = workspace:Raycast(mob.Position,mob.HumanoidRootPart.CFrame.LookVector * closeDistance, raycastParams)

if raycastResult then
	-- This is where I'm not sure what to do - need to find a new targetPos that's parallel to the wall
end
		
-- Move towards the chosen position
mob.Humanoid:MoveTo(targetPos)

UPDATE

I’m getting closer. I can get the orientation of the wall and then just use CFrame.Angles to set the mob’s orientation to either that orientation or the orientation + 180 depending on what the angle is when the mob is facing the wall. As below (just from the if part). However, now I’m unsure how to actually move to mob in that direction he is facing since I don’t have a point to use in the MoveTo function

if raycastResult then
       local orientation = raycastResult.Instance.Orientation.Y
       if math.abs(orientation - mob.HumanoidRootPart.Orientation.Y) > 90 then
		orientation += 180
	end

	defender.HumanoidRootPart.CFrame = CFrame.new(mob.Position) * CFrame.Angles(0,math.rad(orientation),0)			

end
4 Likes

image
You could use the Normal returned by the raycast to find the orientation of the point and use it to create the new cframe CFrame.new(position, position + normal)

2 Likes

That looks a lot cleaner - I’ll give it a shot. Thank you!

1 Like

I realized my code wasn’t actually solving the problem. The way I had my mob moving, it calculated a position vector opposite of a target (so running away) every 1/10 of a second and then used the mob.Humanoid:MoveTo() to walk in that direction. That worked fine. When it approached a wall however it was supposed to move parallel to it rather than keep trying to get to that target. With your help I was able to set up the CFrame of the mob so it is facing parallel to the wall, but I don’t know how to then create a new target position for the MoveTo command. Now that I have the right CFrame to face, how can tell the mob to move in that direction until the next 1/10 sec tick hits and it checks for direction again?