Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Moving a model within a grid?

Asked by
legosweat 334 Moderation Voter
8 years ago

This script is able to move, rotate, and place a model. The item gets placed on a 5x5 block grid.

QUESTION: How will I be able to keep the item within the grid (There are limitations where the item can go); It needs to stay on the grid.

I need the proper way of doing this, I've already tried to make it move only if the mouse's target was the grid; but it was fairly inaccurate as it was slightly still going off the grid.

Localscript:

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local PlacedPart = false
local Selected = true
local Rotated = false
local Rotate = 1.57
local Rotation
local yAxis = 0.6
local Enabled = true
local Item = game.Workspace.Model

Mouse.Button1Down:connect(function()
    if Enabled and not PlacedPart then
        if Item ~= nil then
            ClearSelection()
        end
    end
end)

--[[
function Check()

end
--]]

Mouse.Button2Down:connect(function()
    if not Rotated or Rotation == 0 then
    Rotation = 1
    Rotated = true
    Item:SetPrimaryPartCFrame(CFrame.new(Item.PrimaryPart.Position) * CFrame.fromEulerAnglesXYZ(0,Rotate*Rotation,0))

    elseif Rotation == 1 then
    Rotation = 2
    Rotated = true
    Item:SetPrimaryPartCFrame(CFrame.new(Item.PrimaryPart.Position) * CFrame.fromEulerAnglesXYZ(0,Rotate*Rotation,0))

    elseif Rotation == 2 then
    Rotation = 3
    Rotated = true
    Item:SetPrimaryPartCFrame(CFrame.new(Item.PrimaryPart.Position) * CFrame.fromEulerAnglesXYZ(0,Rotate*Rotation,0))

    elseif Rotation == 3 then
    Rotation = 0
    Item:SetPrimaryPartCFrame(CFrame.new(Item.PrimaryPart.Position) * CFrame.fromEulerAnglesXYZ(0,Rotate*Rotation,0))
    end
end)

function ClearSelection()
    PlacedPart = false
    Selected = false
    Rotated = false
    Item = nil
end

Mouse.Move:connect(function()
    if Selected and not PlacedPart and Enabled then
        if Item ~= nil then
                if Rotated then -- Mouse.Hit.p.X - Mouse.Hit.p.X%25, Spawn.Position.Y, Mouse.Hit.p.Z - Mouse.Hit.p.Z%25
                Item:SetPrimaryPartCFrame(CFrame.new(Mouse.Hit.p.X - Mouse.Hit.p.X%5, yAxis, Mouse.Hit.p.Z - Mouse.Hit.p.Z%5)*CFrame.fromEulerAnglesXYZ(0,Rotate*Rotation,0))
                else
                Item:SetPrimaryPartCFrame(CFrame.new(Mouse.Hit.p.X - Mouse.Hit.p.X%5, yAxis, Mouse.Hit.p.Z - Mouse.Hit.p.Z%5))
            end
        end
    end
end)
0
I better way to rotate/move would be appreciated also! legosweat 334 — 8y

1 answer

Log in to vote
0
Answered by
BlackJPI 2658 Snack Break Moderation Voter Community Moderator
8 years ago

You can utilize the math function min and max here. These functions will return to you either the maximum or minimum of the variant number of arguments given to it.

But we don't want the minimum value or the maximum value, we want a value "clamped" between the two. With a little logic, we get this:

local function clamp(n, low, high)
    return math.min(math.max(n, low), high)
end

However, if the high and low values are supplied the wrong way, this would give the wrong answer. You can do a little check to fix this issue, however it is not necessary if it is used correctly.

local function clamp(n, a, b)
    local temp = a
    a, b = math.min(a, b), math.max(temp, b)
    return math.min(math.max(n, a), b)
end

You can use this function to return to you your x and z coordinates clamped between the bounds you want.

Usage Example:

-- Assuming function clamp is defined
local highBounds = Vector3.new(100, 0, 100)
local lowBounds = Vector3.new(-100, 0, -100)

local part = game.Workspace.Part -- Example object

local x = clamp(part.Position.X, lowBounds.X, highBounds.X)
local y = clamp(part.Position.Y, lowBounds.Y, highBounds.Y)
local z = clamp(part.Position.Z, lowBounds.Z, highBounds.Z)
part.Position = Vector3.new(x, y, z)

So if desired Part position was Vector3.new(132, -32, -92) then the part would be positioned at Vector3.new(100, 0, -92).

0
What would n, a, and b represent? Sorry, really new to the math portion of lua legosweat 334 — 8y
0
Sorry, n is the value you want to clamp, a and b are your upper and lower limits BlackJPI 2658 — 8y
0
Need more help on this @BlackJPI legosweat 334 — 8y
0
What are you having trouble with? BlackJPI 2658 — 8y
View all comments (2 more)
0
@BlackJPI, So the n is a value. would that be a position or something? Also, the a, and b; would the values be positions? AND lastly, how can I use this in my script? legosweat 334 — 8y
0
Check my edit, you could use this in your script by clamping your calucluated x and z values when setting the model CFrame BlackJPI 2658 — 8y
Ad

Answer this question