How do I make a crack effect?

I am making an effect when the player has jumped and landed onto the ground, a part will appear on the floor with a crack decal but the problem is I don’t know how to do the CFrame

CrackEffect.rbxm (3.5 KB)

Code
local PlayerS = game:GetService('Players')

local DebrisS = game:GetService('Debris')

local EnumMaterial = Enum.Material

local Materials = {
	[EnumMaterial.Air] = true,
	[EnumMaterial.Water] = true,
}

local CrackPart = script:WaitForChild('Crack')

PlayerS.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(Cha)
		local Hum = Cha:WaitForChild('Humanoid')
		local HRP = Cha:WaitForChild('HumanoidRootPart')
		local IsFalling
		local DB
		Hum.FreeFalling:Connect(function(Falling)
			local LowestFallHeight = 15 -- if studs fallen more than LowestFallHeight then it will create crack
			IsFalling = Falling -- make sure that the Humanoid is falling for the first time
			if IsFalling and not DB then
				DB = true
				local MaxHeight = 0
				while IsFalling do
					local Hight = math.abs(HRP.Position.y)
					if Hight > MaxHeight then
						MaxHeight = Hight
					end
					wait()
				end
				local FallHeight = MaxHeight - HRP.Position.y
				-- if the player fell a longer distance than LowestFallHeight and the Material the Humanoid is on top of isn't air and water then create crack
				if FallHeight > LowestFallHeight and not Materials[Hum.FloorMaterial] then
					local CrackPart_Clone = CrackPart:Clone()
					local Weld = Instance.new('Weld')
					CrackPart_Clone.Parent = Cha
					Weld.Parent = workspace
					Weld.Part0 = CrackPart_Clone
					Weld.Part1 = workspace.Baseplate
					Weld.C0 = HRP.CFrame - HRP.CFrame.p - Vector3.new(0,2.5,0)
					DebrisS:AddItem(CrackPart_Clone,1)
				end
				DB = false
			end
		end)
		Hum.JumpPower = 100
	end)
end)

with that code, it always make the crack at 0,0,0

3 Likes

What I have done is i have created a simple raycast that fires down and places the crack on the part that it hits. I have tested this in APS so this should work :smile:

local PlayerS = game:GetService('Players')

local DebrisS = game:GetService('Debris')

local EnumMaterial = Enum.Material

local Materials = {
	[EnumMaterial.Air] = true,
	[EnumMaterial.Water] = true,
}

local CrackPart = script:WaitForChild('Crack')

PlayerS.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(Cha)
		local Hum = Cha:WaitForChild('Humanoid')
		local HRP = Cha:WaitForChild('HumanoidRootPart')
		local IsFalling
		local DB
		Hum.FreeFalling:Connect(function(Falling)
			local LowestFallHeight = 15 -- if studs fallen more than LowestFallHeight then it will create crack
			IsFalling = Falling -- make sure that the Humanoid is falling for the first time
			if IsFalling and not DB then
				DB = true
				local MaxHeight = 0
				while IsFalling do
					local Hight = math.abs(HRP.Position.y)
					if Hight > MaxHeight then
						MaxHeight = Hight
					end
					wait()
				end
				local FallHeight = MaxHeight - HRP.Position.y
				-- if the player fell a longer distance than LowestFallHeight and the Material the Humanoid is on top of isn't air and water then create crack
				if FallHeight > LowestFallHeight and not Materials[Hum.FloorMaterial] then
					local CrackPart_Clone = CrackPart:Clone()
					local Weld = Instance.new('Weld')
					CrackPart_Clone.Parent = Cha
					Weld.Parent = workspace
					Weld.Part0 = CrackPart_Clone
					Weld.Part1 = workspace.Baseplate
					
					--Raycasting
					local ray = Ray.new(HRP.CFrame.p,Vector3.new(0,-20,0))
					local part,position,surfaceNormal = workspace:FindPartOnRay(ray,Cha)
					local cross = Vector3.new(0,1,0):Cross(surfaceNormal)
					local angle = math.asin(cross.magnitude)
					CrackPart_Clone.CFrame = CFrame.new(position + surfaceNormal - Vector3.new(0,1,0))
					*CFrame.fromAxisAngle(cross.magnitude == 0 and Vector3.new(1) or cross.unit, angle)
					--
					
					DebrisS:AddItem(CrackPart_Clone,1)
				end
				DB = false
			end
		end)
		Hum.JumpPower = 100
	end)
end)

I have amended this to work with normals.

9 Likes

That doesn’t account for slopes or otherwise uneven surfaces. To make a crack appear correctly on most surfaces you need to use the surface normal, as explained here;

http://wiki.roblox.com/index.php?title=User:EgoMoose/The_scary_thing_known_as_the_cross_product#An_example_of_the_cross_product_in_use:_rotated_placement

5 Likes

I have corrected the original post to work with surface normals. Thanks!

1 Like

Thank you so much @RedDuck765 & @TaaRt

I works flawlessly!!

1 Like