I am trying to recreate the trowel for my game, the trowel is placed down fine, but the issue is that I’m not sure how to recreate the calculation for rotation in the trowel script.
To me, it seems like the script is getting the direction of the player relative to the mouse position so that it can figure out which direction it should place the trowel down. But this issue with this is the snap() function because I’m not sure how it’s calculated.
local lookAt = snap( (targetPos - character.Head.Position).unit )
This is what snap looks like, but I’m not sure how the formula (snap()) works.
function snap(v)
if math.abs(v.x)>math.abs(v.z) then
if v.x>0 then
return Vector3.new(1,0,0)
else
return Vector3.new(-1,0,0)
end
else
if v.z>0 then
return Vector3.new(0,0,1)
else
return Vector3.new(0,0,-1)
end
end
end
I guess it’s supposed to be for CFrame.lookAt, but I’m confused because the next line uses CFrame.new() but still has the same structure, so will it still work?
local cf = CFrame.new(targetPos, targetPos + lookAt)
I’m also not sure how the wall is built and the math behind that either. What does assert() do? How is each brick calculated?
function buildWall(cf)
local color = BrickColor.Random()
local bricks = {}
assert(wallWidth>0)
local y = 0
while y < wallHeight do
local p
local x = -wallWidth/2
while x < wallWidth/2 do
local brick
brick, p = placeBrick(cf, Vector3.new(x, y, 0), color)
x = p.x
table.insert(bricks, brick)
wait(brickSpeed)
end
y = p.y
end
return bricks
end
If you need it, this is the full script for the trowel.
local wallHeight = 4
local brickSpeed = 0.04
local wallWidth = 12
local Tool = script.Parent
local MouseLoc = Tool:WaitForChild("MouseLoc",10)
-- places a brick at pos and returns the position of the brick's opposite corner
function placeBrick(cf, pos, color)
local brick = Instance.new("Part")
brick.BrickColor = color
brick.CFrame = cf * CFrame.new(pos + brick.Size / 2)
script.Parent.BrickCleanup:Clone().Parent = brick -- attach cleanup script to this brick
brick.BrickCleanup.Disabled = false
brick.Parent = game.Workspace
brick:MakeJoints()
return brick, pos + brick.Size
end
function buildWall(cf)
local color = BrickColor.Random()
local bricks = {}
assert(wallWidth>0)
local y = 0
while y < wallHeight do
local p
local x = -wallWidth/2
while x < wallWidth/2 do
local brick
brick, p = placeBrick(cf, Vector3.new(x, y, 0), color)
x = p.x
table.insert(bricks, brick)
wait(brickSpeed)
end
y = p.y
end
return bricks
end
function snap(v)
if math.abs(v.x)>math.abs(v.z) then
if v.x>0 then
return Vector3.new(1,0,0)
else
return Vector3.new(-1,0,0)
end
else
if v.z>0 then
return Vector3.new(0,0,1)
else
return Vector3.new(0,0,-1)
end
end
end
Tool.Enabled = true
function onActivated()
if not Tool.Enabled then
return
end
Tool.Enabled = false
local character = Tool.Parent;
local humanoid = character:FindFirstChildOfClass("Humanoid")
if not humanoid then
return
end
local targetPos = MouseLoc:InvokeClient(game:GetService("Players"):GetPlayerFromCharacter(character))
local lookAt = snap( (targetPos - character.Head.Position).unit )
local cf = CFrame.new(targetPos, targetPos + lookAt)
Tool.Handle.BuildSound:Play()
buildWall(cf)
wait(5)
Tool.Enabled = true
end
Tool.Activated:Connect(onActivated)
