What I want to do is have a roach that moves along the surfaces of walls like a bug would. The idea is that before it moves, it raycasts forward to see if there’s a wall in the way. If there is, it’ll cling to that surface and continue its path.
One of the problems I have is that the orientation of the part isn’t really preserved when positioning it onto another surface.
--[[Variables]]--
--Services--
local random = Random.new();
--Roach--
local roach = script.Parent;
local movementDirection = Vector3.new();
local stepSize = 0.1;
local stepSpeed = 0.005;
local raycastParams = RaycastParams.new();
raycastParams.FilterDescendantsInstances = {};
raycastParams.FilterType = Enum.RaycastFilterType.Whitelist;
raycastParams.RespectCanCollide = true;
--[[Functions]]--
function findWallToClingTo()
local origin = roach.Position;
local direction = roach.CFrame.LookVector * stepSize;
return workspace:Raycast(origin, direction, raycastParams);
end
function clingToSurface(result)
local yRot = roach.Orientation.Y;
roach.CFrame = CFrame.lookAt(result.Position, result.Position - result.Normal);
task.wait(1);
roach.CFrame *= CFrame.Angles(math.rad(90), 0, 0);
roach.CFrame += roach.CFrame.UpVector * roach.Size.Y / 2;
task.wait(1);
--roach.CFrame *= CFrame.Angles(0, math.rad(yRot), 0);
--task.wait(1);
end
function step()
local wallResult = findWallToClingTo();
if wallResult then
clingToSurface(wallResult);
return;
end
roach.Position += roach.CFrame.LookVector * stepSize;
end
while true do
step();
task.wait(stepSpeed);
end