I definitely didn’t think of the Normal * Distance
thing, I’ll give that a shot. I also didn’t think constant degree/radian conversion had any consequences. A Normals
array I didn’t think of either, so I’ll try and implement that.
Here were the three different times I tried to implement it with the purpose of detecting the second wall (Before you replied with a potential solution).
The Eyesore
-- 1, __incomingresults1 & 2 are raycast left & right from normal
if __IncomingResults1 or __IncomingResults2 then
__WorkingResults = __IncomingResults1 or __IncomingResults2
local Divider = RootPart.CFrame.LookVector
local Object: Part, Offset = __StateResults.Instance, __StateResults.Instance.CFrame:PointToObjectSpace(RootPart.Position)
local PositionRelativeTo = Vector3.new(math.clamp(Offset.X, -(Object.Size/2) .X, (Object.Size/2).X), math.clamp(Offset.Y, -(Object.Size/2).Y, (Object.Size/2).Y), math.clamp(Offset.Z, -(Object.Size/2).Z, (Object.Size/2).Z))
local PreciseSurfaceDirection3 = (Object.CFrame * CFrame.new(PositionRelativeTo)) - RootPart.Position
Divider = Vector3.new(Divider.X, 0, Divider.Z).Unit
local PreciseSurfaceDirection = Vector3.new(PreciseSurfaceDirection3.X, 0, PreciseSurfaceDirection3.Z).Unit
local Intersect = Divider:Cross(PreciseSurfaceDirection)
__WallCorrection2 = 180 - math.deg(math.acos(RootPart.CFrame.LookVector.Unit:Dot(__StateResults.Normal.Unit)))
local __FutureCorrection2
if math.deg(Intersect.Y) < 0 then -- Left
__FutureCorrection2 = __WallCorrection2
__FinalDiam2 = math.rad(math.abs(math.round(__WallCorrection2 - 90)))
else -- Right
__FutureCorrection2 = -__WallCorrection2
__FinalDiam2 = math.rad(math.round(__WallCorrection2 - 90))
end
end
if math.deg(__FinalDiam - __FinalDiam2) == 0 and __StateResults ~= __WorkingResults then
__WallCorrection = __WallCorrection - __WallCorrection2
__FinalDiam = __FinalDiam - __FinalDiam2
end
local __IncomingResults
for i = DirectionInt, DirectionInt2 do
__IncomingResults = workspace:Raycast(RootPart.Position, (RootPart.CFrame * CFrame.Angles(0, i, 0)).LookVector * 5, __StateConditions)
if __IncomingResults and __IncomingResults.Instance ~= __StateResults.Instance then
print(DirectionInt, DirectionInt2, __IncomingResults.Instance, __StateResults.Instance)
break
end
end
-- 2
local __Part1Results = workspace:Raycast(RootPart.Position, (RootPart.CFrame * CFrame.Angles(0, math.rad(__FutureCorrection), 0)).LookVector * 5, __StateConditions)
local __Part2Results
local Alt1, Alt2 = 90, 0
if __Part1Results then
__Part2Results = workspace:Raycast(__Part1Results.Position, (CFrame.new(__Part1Results.Position) * CFrame.Angles(0, -__FutureCorrection, 0)).LookVector * 3, __StateConditions)
if __Part2Results then
print(__Part2Results.Instance)
local Divider = RootPart.CFrame.LookVector
local Object: Part, Offset = __Part2Results.Instance, __Part2Results.Instance.CFrame:PointToObjectSpace(RootPart.Position)
local PositionRelativeTo = Vector3.new(math.clamp(Offset.X, -(Object.Size/2) .X, (Object.Size/2).X), math.clamp(Offset.Y, -(Object.Size/2).Y, (Object.Size/2).Y), math.clamp(Offset.Z, -(Object.Size/2).Z, (Object.Size/2).Z))
local PreciseSurfaceDirection = (Object.CFrame * CFrame.new(PositionRelativeTo)) - RootPart.Position
Divider = Vector3.new(Divider.X, 0, Divider.Z).Unit
PreciseSurfaceDirection = Vector3.new(PreciseSurfaceDirection.X, 0, PreciseSurfaceDirection.Z).Unit
local Intersect = Divider:Cross(PreciseSurfaceDirection)
Alt1 = 180 - math.deg(math.acos(RootPart.CFrame.LookVector.Unit:Dot(__Part2Results.Normal.Unit)))
if math.deg(Intersect.Y) < 0 then
Alt2 = math.rad(math.abs(math.round(Alt1 - 90)))
Direction2 = "Left"
else
Alt2 = math.rad(math.round(Alt1 - 90))
Direction2 = "Right"
end
--print(Direction, Direction2)
--print(__FinalDiam - Alt1)
end
end
-- 3
local __RoomResults = workspace:Raycast(RootPart.Position, ((RootPart.CFrame * CFrame.new(0, 0, -0.5)) * CFrame.Angles(0, __FinalDiam, 0)).LookVector * 2, __StateConditions)
local Alt1, Alt2 = 90, 0
if __RoomResults then
local Divider, Direction2 = RootPart.CFrame.LookVector, nil
local Object: Part, Offset = __RoomResults.Instance, __RoomResults.Instance.CFrame:PointToObjectSpace(RootPart.Position)
local PositionRelativeTo = Vector3.new(math.clamp(Offset.X, -(Object.Size/2) .X, (Object.Size/2).X), math.clamp(Offset.Y, -(Object.Size/2).Y, (Object.Size/2).Y), math.clamp(Offset.Z, -(Object.Size/2).Z, (Object.Size/2).Z))
local PreciseSurfaceDirection = (Object.CFrame * CFrame.new(PositionRelativeTo)) - RootPart.Position
Divider = Vector3.new(Divider.X, 0, Divider.Z).Unit
PreciseSurfaceDirection = Vector3.new(PreciseSurfaceDirection.X, 0, PreciseSurfaceDirection.Z).Unit
local Intersect = Divider:Cross(PreciseSurfaceDirection)
Alt1 = 180 - math.deg(math.acos(RootPart.CFrame.LookVector.Unit:Dot(__RoomResults.Normal.Unit)))
if math.deg(Intersect.Y) < 0 then
Alt2 = math.rad(math.abs(math.round(Alt1 - 90)))
Direction2 = "Left"
else
Alt2 = math.rad(math.round(Alt1 - 90))
Direction2 = "Right"
end
if Direction == "Right" and Direction2 == "Left" then
SpeedZ.Raw = 0
elseif Direction2 == "Right" and Direction == "Left" then
SpeedZ.Raw = 0
end
end