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
9 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:

01local Player = game.Players.LocalPlayer
02local Mouse = Player:GetMouse()
03local PlacedPart = false
04local Selected = true
05local Rotated = false
06local Rotate = 1.57
07local Rotation
08local yAxis = 0.6
09local Enabled = true
10local Item = game.Workspace.Model
11 
12Mouse.Button1Down:connect(function()
13    if Enabled and not PlacedPart then
14        if Item ~= nil then
15            ClearSelection()
View all 65 lines...
0
I better way to rotate/move would be appreciated also! legosweat 334 — 9y

1 answer

Log in to vote
0
Answered by
BlackJPI 2658 Snack Break Moderation Voter Community Moderator
9 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:

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

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.

1local function clamp(n, a, b)
2    local temp = a
3    a, b = math.min(a, b), math.max(temp, b)
4    return math.min(math.max(n, a), b)
5end

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

Usage Example:

01-- Assuming function clamp is defined
02local highBounds = Vector3.new(100, 0, 100)
03local lowBounds = Vector3.new(-100, 0, -100)
04 
05local part = game.Workspace.Part -- Example object
06 
07local x = clamp(part.Position.X, lowBounds.X, highBounds.X)
08local y = clamp(part.Position.Y, lowBounds.Y, highBounds.Y)
09local z = clamp(part.Position.Z, lowBounds.Z, highBounds.Z)
10part.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 — 9y
0
Sorry, n is the value you want to clamp, a and b are your upper and lower limits BlackJPI 2658 — 9y
0
Need more help on this @BlackJPI legosweat 334 — 9y
0
What are you having trouble with? BlackJPI 2658 — 9y
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 — 9y
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 — 9y
Ad

Answer this question