generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripting Helpers
Home Search
 

Re: Making an object snap to a grid

Previous Thread :: Next Thread 
tinarg is not online. tinarg
Joined: 18 Jun 2010
Total Posts: 4925
11 Jul 2014 09:19 PM
I can use math.floor() on X and Z, but I need a part to snap to a weird number like 0.4 or 1.2 on the Y axis. How would I do this?
Report Abuse
sbk28 is not online. sbk28
Joined: 15 Nov 2008
Total Posts: 2528
11 Jul 2014 09:20 PM
local number = Amount % 0.4 -- (or 1.2)
local number = (Amount - number)
Report Abuse
tinarg is not online. tinarg
Joined: 18 Jun 2010
Total Posts: 4925
11 Jul 2014 09:24 PM
What does amount stand for?
Report Abuse
sbk28 is not online. sbk28
Joined: 15 Nov 2008
Total Posts: 2528
11 Jul 2014 09:26 PM
Amount would be your X or Z


% is modulus so it returns a remainder after division

for example

3 % 2 = 1

4 % 2 = 0

therefore if your x position was 0.9 and you wanted to move it to the nearest 0.4 ( so 0.8)

0.9 % 0.4 = 0.1

0.9 - 0.1 = 0.8
Report Abuse
tinarg is not online. tinarg
Joined: 18 Jun 2010
Total Posts: 4925
11 Jul 2014 09:34 PM
I tried this but it teleported the brick really far away...

mouse.Hit.Y % Increment



Increment is either 1, 0.4, 0.2, or 1.2
Report Abuse
sbk28 is not online. sbk28
Joined: 15 Nov 2008
Total Posts: 2528
11 Jul 2014 09:35 PM
http://wiki.roblox.com/index.php?title=Arithmetic_operator
Report Abuse
tinarg is not online. tinarg
Joined: 18 Jun 2010
Total Posts: 4925
11 Jul 2014 09:39 PM
Ehh... I tried using what it said and tried these... none of them work:


ClonedBrick.Position.Y-(mouse.Hit.Y % Increment)

ClonedBrick.Position.Y+(mouse.Hit.Y % Increment)

(mouse.Hit.Y % Increment)

Report Abuse
tinarg is not online. tinarg
Joined: 18 Jun 2010
Total Posts: 4925
11 Jul 2014 09:47 PM
bump
Report Abuse
tinarg is not online. tinarg
Joined: 18 Jun 2010
Total Posts: 4925
11 Jul 2014 09:53 PM
bump
Report Abuse
tinarg is not online. tinarg
Joined: 18 Jun 2010
Total Posts: 4925
11 Jul 2014 10:21 PM
bump
Report Abuse
MHebes is not online. MHebes
Joined: 04 Jan 2013
Total Posts: 2278
11 Jul 2014 10:34 PM
function SnapToGrid(part, grid)
local pos = part.Position
return Vector3.new(pos.X-pos.X%grid.X, pos.Y-pos.Y%grid.Y, pos.Z-pos.Z%grid.Z)
end

local part = Workspace.Part
SnapToGrid(part, Vector3.new(1, 0.4, 1))



That seems to work. 'part' is the part to snap, 'grid' is what number to snap to on each axis, a Vector3 value.
Report Abuse
tinarg is not online. tinarg
Joined: 18 Jun 2010
Total Posts: 4925
12 Jul 2014 04:27 PM
Ooh, sparkly. Lemme try it.
Report Abuse
tinarg is not online. tinarg
Joined: 18 Jun 2010
Total Posts: 4925
12 Jul 2014 04:46 PM


Eh, I tried this, but it didn't work:

function SnapToGrid(part, grid)
local pos = ClonedBrick.Position
return Vector3.new(pos.X-pos.X%grid.X, pos.Y-pos.Y%grid.Y, pos.Z-pos.Z%grid.Z)
end

SnapToGrid(ClonedBrick, Vector3.new(1, 0.4, 1))



ClonedBrick is the part I want to snap to a grid.
Report Abuse
tinarg is not online. tinarg
Joined: 18 Jun 2010
Total Posts: 4925
12 Jul 2014 04:52 PM
bump
Report Abuse
MHebes is not online. MHebes
Joined: 04 Jan 2013
Total Posts: 2278
12 Jul 2014 04:57 PM
Oops, I think I combined two different scripts in my head.

function SnapToGrid(part, grid)
local pos = part.Position
part.Position = (pos.X-pos.X%grid.X, pos.Y-pos.Y%grid.Y, pos.Z-pos.Z%grid.Z)
end
Report Abuse
tinarg is not online. tinarg
Joined: 18 Jun 2010
Total Posts: 4925
12 Jul 2014 05:11 PM
Where do I tell the grid what to snap by? Would I replace pos with Vector3.new(1,0.4,1)?
Report Abuse
tinarg is not online. tinarg
Joined: 18 Jun 2010
Total Posts: 4925
12 Jul 2014 05:16 PM
bump
Report Abuse
MHebes is not online. MHebes
Joined: 04 Jan 2013
Total Posts: 2278
12 Jul 2014 05:17 PM
No... That's a function that I made so it would be easy for you.

SnapToGrid( part, grid )

When you call the function, just change that second argument to Vector3.new(1, 0.4, 1)

So like

SnapToGrid(Workspace.Part, Vector3.new(1, 0.4, 1))
Report Abuse
tinarg is not online. tinarg
Joined: 18 Jun 2010
Total Posts: 4925
12 Jul 2014 05:27 PM
It works! Thanks.

I really need to learn how to use functions better... I learned everything out of order, so I know advanced topics fluently while I lack at some of the simpler ones.
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripting Helpers
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image