Part going through the floor

I’m not sure if this is an issue with my game, but for some odd reason my enemy is fazing through the floor.

Place:
Testing.rbxl (48.6 KB)

Full Code:

task.wait(2)
local Points = workspace.Points
local Enemy = workspace.Enemy
local Node = 1
local Speed = 5
local OverallDeltaTime = 0
local CornerStatus = 0
local Turning = false
local Repeat = false

--Services

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")

--FunctionsDefine

--MathFunctions

local function Lerp(a, b, t)
	return a + (b - a) * t
end

local function QuadraticBezier(t, p0, p1, p2)
	local l1 = Lerp(p0, p1, t)
	local l2 = Lerp(p1, p2, t)
	local quad = Lerp(l1, l2, t)
	return quad
end

local function getpos(Pos)
	return Vector3.new(Pos.X, 0, Pos.Z)
end

local function InitiateGraphicalTurningSequenceLoop(DeltaTime, Time, Distance, Position, LookPositionX, LookPositionY, EndCFrame)
	CornerStatus += DeltaTime
	
	Time = CornerStatus / Distance
	if Time > 1 then
		Enemy.CFrame = EndCFrame
		CornerStatus = 0
		RunService:UnbindFromRenderStep("CornerTurn")
		if Repeat == true then
			if Node >= 6 then
				Node = 2
			end
		end
		Node += 1
		OverallDeltaTime = 0
		Turning = false
		return
	else
		Position = QuadraticBezier(Time, Points["Point"..Node].Position, Points["Point"..Node].Mid.Position, Points["Point"..Node].End.Position)
		LookPositionX, LookPositionY = CFrame.new(Position, QuadraticBezier(Time + 0.001, Points["Point"..Node].Position, Points["Point"..Node].Mid.Position, Points["Point"..Node].End.Position)):ToOrientation()
		Enemy.CFrame = CFrame.new(Position) * CFrame.fromOrientation(LookPositionX, LookPositionY, 0)
		local Part = Instance.new("Part")
		Part.Anchored = true
		Part.CanCollide = false
		Part.CanTouch = false
		Part.CanQuery = false
		Part.Color = Color3.fromRGB(255, 0, 0)
		Part.Material = Enum.Material.Neon
		Part.Parent = workspace
		Part.Size = Vector3.new(0.2, 0.2, 0.2)
		Part.CFrame = CFrame.new(Position) * CFrame.fromOrientation(LookPositionX, LookPositionY, 0)
	end
end

local function InitiateGraphicalTurningSequence()
	Enemy.CFrame = Points["Point"..Node].CFrame

	local Time = 0
	local Position = 0
	local LookPositionX = 0
	local LookPositionY = 0
	
	
	local Distance = 
		(getpos(Points["Point"..Node].Position) - getpos(Points["Point"..Node].Mid.Position)).Magnitude / Speed +
		(getpos(Points["Point"..Node].Mid.Position) - getpos(Points["Point"..Node].End.Position)).Magnitude / Speed
	

	local EndCFrame = Points["Point"..Node].End.CFrame
	RunService:BindToRenderStep("CornerTurn", 0, function(DeltaTime)
		InitiateGraphicalTurningSequenceLoop(DeltaTime, Time, Distance, Position, LookPositionX, LookPositionY, EndCFrame, Node)
	end)
end

RunService.RenderStepped:Connect(function(DeltaTime)	
	if Turning == false then
		local Part = Instance.new("Part")
		Part.Anchored = true
		Part.CanCollide = false
		Part.CanTouch = false
		Part.CanQuery = false
		Part.Color = Color3.fromRGB(255, 0, 0)
		Part.Material = Enum.Material.Neon
		Part.Parent = workspace
		Part.Size = Vector3.new(0.2, 0.2, 0.2)
		Part.CFrame = CFrame.new(Lerp(Points["Point"..Node - 1].End.Position,
			Points["Point"..Node].Position,
			(OverallDeltaTime + DeltaTime/2) / ((getpos(Points["Point"..Node - 1].End.Position) - getpos(Points["Point"..Node].Position)).Magnitude / Speed)
			)
		) * CFrame.fromOrientation(CFrame.new(Points["Point"..Node - 1].End.Position, Points["Point"..Node].Position):ToOrientation())
		OverallDeltaTime += DeltaTime
		Enemy.CFrame = CFrame.new(Lerp(Points["Point"..Node - 1].End.Position,
			Points["Point"..Node].Position,
			OverallDeltaTime / ((getpos(Points["Point"..Node - 1].End.Position) - getpos(Points["Point"..Node].Position)).Magnitude / Speed)
			)
		) * CFrame.fromOrientation(CFrame.new(Points["Point"..Node - 1].End.Position, Points["Point"..Node].Position):ToOrientation())
		if Points["Point"..Node].CFrame:ToWorldSpace():ToObjectSpace(Enemy.CFrame).Position.Z <= 0 then
			OverallDeltaTime = 0
			if Points["Point"..Node]:FindFirstChild("Mid") then
				Turning = true
				coroutine.wrap(InitiateGraphicalTurningSequence)()
			else
				if Node == #Points:GetChildren() - 1 then
					warn("Enemy has gotten through")
					Node = 3
					Repeat = true
				else
					Node += 1
				end
			end
		end
	end
end)

Video:

As you can see, the highlight outline is in the floor.

1 Like