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