Hello! Again. (or if not, welcome.) And I post daily scripting, building, developing, ect problems.
Today’s problem is kind of the same as last times. Still. Like the last one, I will give out another thanks. It is your chance. (I think).
When I move my mouse it appears a brick. And it follows your every move. Like build a boat, building tool but without the mouse. Thanks to these two people, “roasthistorychicken” and “Danrich123” for helping me with the Y axis. Here’s another example. (It is kinda laggy.)
Please help and respond.
script:
local player = game.Players.LocalPlayer
local workspacepart = game:GetService("Workspace")
local partsgo = Instance.new("Model")
local playerbuild = Instance.new("Model")
playerbuild.Parent = workspacepart
playerbuild.Name = (player.Name.." Space")
partsgo.Name = "ModelParts"
partsgo.Parent = workspacepart
local madepart = Instance.new("Part")
madepart.Parent = partsgo
madepart.Size = Vector3.new(3,3,3)
madepart.Name = (player.Name.." Part")
madepart.Anchored = true
local mouse = player:GetMouse()
mouse.TargetFilter = madepart; --ignores the part you're moving (so it doesn't make a weird thing
while true do
wait()
--madepart.CFrame = CFrame.new(mouse.Hit.X, mouse.Hit.Y, mouse.Hit.Z) + Vector3.new(0, madepart.Size.Y / 2, 0)
madepart.CFrame = CFrame.new(math.ceil(mouse.Hit.p.X), 3, math.ceil(mouse.Hit.p.Z))
end
PLEASE READ:
When you fix the X and Z positions, there might be another problem, too. See how you had to add a + at the mouse.hit.Y? When you hover your cursor over a roof for example, a house, it will probably go up. Please fix.
PROBLEM:
When the mouse moves to a X and Z position, it won’t go up. Same with a roof. Also collides with wall.
What do you mean by, “Roof”? If your are saying what will happen, then the same thing will happen on the roof and on the floor. The part goes to the mouse position.
local player = game.Players.LocalPlayer
local partFolder = Instance.new("Folder") -- folders are better
partFolder.Name = "ModelParts"
partFolder.Parent = workspace
local playerBuild = Instance.new("Folder")
playerBuild.Parent = workspace
playerBuild.Name = player.Name .. " Space"
local moveModel = Instance.new("Model")
moveModel.Parent = partFolder
local part = Instance.new("Part") -- turned into a model just for you, it works significantly better
part.Parent = moveModel
part.Size = Vector3.new(3, 3, 3)
part.Name = player.Name .. " Part"
part.Anchored = true
moveModel.PrimaryPart = part
local mouse = player:GetMouse()
local function ceilVector3(vec)
return Vector3.new(math.ceil(vec.X), math.ceil(vec.Y), math.ceil(vec.Z))
end
function placeAtMouse(dt) -- egomoose's solution
if (mouse.Target) then
-- where we cast our ray from (shifted slightly up so the ray will hit the surface we're hovering)
local origin = mouse.Hit.p + Vector3.new(0, 0.1, 0)
-- cast our ray and get our normal, which is a unit vector
local ray = Ray.new(origin, Vector3.new(0, -1, 0))
local hit, pos, normal = workspace:FindPartOnRay(ray, moveModel)
-- get the perpendicular vector and the angle between the two
local cross = Vector3.new(0, 1, 0):Cross(normal)
local theta = math.acos(Vector3.new(0, 1, 0):Dot(normal))
-- make sure our axis is valid
local axis = Vector3.new(1, 0, 0)
if (cross.magnitude > 0) then
axis = cross.unit
end
-- CFrame the model
moveModel:SetPrimaryPartCFrame(
CFrame.new(mouse.Hit.p + normal * moveModel:GetBoundingBox().Y / 2) -- position
* CFrame.fromAxisAngle(axis, theta) -- rotation
)
end
end
-- update every frame
game:GetService("RunService").RenderStepped:connect(placeAtMouse)
Edit: Modified it to do the whole math.ceil thing.