#i need help with rotating a part (making a grid placement system)
1 messages · Page 1 of 1 (latest)
so this is normal it sohuld be like this
but when i rotate the part
not only does it go off the boundries but
nouse its not even at the correct location
how it should be is this
basically how i want it to be
cframe = CFrame.new(grid.Position + Vector3.new(0,itemToSpawn.PrimaryPart.Size.Y/2,0)) * CFrame.Angles(0, math.rad(Rotation), 0)
this is the part that changes its location and rotation
else if input.KeyCode == Enum.KeyCode.R then
wait()
Rotation += 90
basically i want rotation without pivoting it to the spot
itemToSpawn:PivotTo(cframe)
this is what i did cause it's a model but i want to rotate it without pivoting
itemToSpawn:SetPrimaryPartCFrame(cframe)
this doesn't work either cause
i have to move it by
pivoting it and
rotating it by setting primarypartcframe
@dusky otter
thank
alrt so
im making a game and i need placement system for it
first i made placement system
similar to tower defense games
okay
and the problem i had with it was the the parts could clip through the walls so i scratched it and decided to go with grid placement which would more suit the gameplay
i used this vid for some basic help and made this in roblox
https://www.youtube.com/watch?v=i9W1kqUinIs
In this Unity tutorial we will place objects on our grid. At the end of this tutorial you will have a grid placement system that you can use in your own project.
House building system in unity project template:
https://courses.sunnyvalleystudio.com/p/house-building-system-in-unity
Starter Project:
https://www.patreon.com/posts/80441380
Script...
if u go to
0:50
thats how parts are setup
as well as the grid parts
so what happends is
when i move
the part
i use PivotTo
when i change parts orientation it still uses pivot to
which instead of the part staying in same place and just rotating
90 degrees
it moves it position to face other way
so
the red drawing here
is how i want it to be
without going ot of bounds
oh interesting
i can send the full script if u want but i just sent the parts that handle rotations
can I see that code again
heres the whole script
idk if u understand it but this is the part that handles rotations
and rays
RS.RenderStepped:Connect(function()
if itemToSpawn then
local result = MouseRaycast({itemToSpawn})
if result and result.Instance then
if result.Instance.Parent.Parent.Name == "House" then
canPlace = true
--ColorPlaceHolderItem(Color3.new(0,1,0))
--local ModelSize = itemToSpawn.PrimaryPart.Size/2
--local ModelPos = itemToSpawn.PrimaryPart.Position
--CheckIntersections(ModelSize, ModelPos, itemToSpawn)
-- if CheckIntersections(ModelSize, ModelPos, itemToSpawn) == false then
-- canPlace = true
-- ColorPlaceHolderItem(Color3.new(0,1,0))
-- end
--else
-- canPlace = false
-- ColorPlaceHolderItem(Color3.new(1,0,0))
--end
x = result.Position.X
y = result.Position.Y
z = result.Position.Z
if result.Instance.Name == "GridPart" then
print("1")
local grid = result.Instance.Parent.gridframe
cframe = CFrame.new(grid.Position + Vector3.new(0,itemToSpawn.PrimaryPart.Size.Y/2,0)) * CFrame.Angles(0, math.rad(Rotation), 0)
--cframe = CFrame.new(result.Instance.Position + Vector3.new(0,itemToSpawn.PrimaryPart.Size.Y/2,0))
local cursorCframe = CFrame.new(x,y,z)
cursorClone.CFrame = cursorCframe
end
itemToSpawn:PivotTo(cframe)
end
end
end
end)
okay yeah
i have lots of comments cause i was trying diff stuff
you ever heard of a guard?
bascially its just when you do like
if not itemToSpawn then return end
and then you don't have to have a big block indented
yes
can make it easier to read
anyways not that relevant
so what you're doing here is making a new cframe half the y size above the grid position and then just rotated via angles. is that intentional?
so 0,itemToSpawn.PrimaryPart.Size.Y/2,0)
this is cause when i hover mouse over grid
the part is half way through round
so we just add another half to it
and CFrame.Angles(0, math.rad(Rotation), 0) this is what i use for rotating it
yeah I see that
I'm going to need a bit more to solve this. I'm still a little confused on what exactly grid is and which number half of Y is, also you said you have the pivot off center?
half of Y is the size right, but wouldn't that change when you rotate it?
oh nvm
yup take all the time u need ill send a vid
that could help 1s
20 seconds
its uploading xd
it should process in like
15 seconds maybe
oh fr
you should just try share x :)
ill get it
not if its small enough
its in 360p but it should be clear enough hopefylly
yt taking long to process
alrt its in
hd now
ah yeah that makes sense hmm I don't think that part of the code is causing it, I think it has to do with whatever gridframe is, can you show me that?
yes
** You are now Level 6! **
if its in the corner wouldn't it only rotate like
it does rotate like that
i have issue with rotation and clipping
wouldn't you want it to rotate more like this?
so why don't we move the pivot to the center right or something
now that u say it i just did that let me test 1s
now this is the issue i had this issue before
basically it takes like
half of 2 grids
okay
thats because you use the pivot of the corner on the grid tile
lets try to align those maybe
?
what did u change
i dont think you're even using that actually
in ur code do :GetPviot() instead of .CFrame
damn didnt know that was a thing what i did is i put
a small part
at where the pivot
should have been
💀
lol
play around with it i guess?
right
here's what i was thinking like 2 ways
so what I would do is when you try to rotate you can change the pivot, or you can save the last working cframe and if you rotate it to one, check collision/in box, and its wrong you can just set it back to the last good one
here's what i did before
so
in the script rn i do
cframe = CFrame.new(grid:GetPivot() + Vector3.new(0,itemToSpawn.PrimaryPart.Size.Y/2,0)) * CFrame.Angles(0, math.rad(Rotation), 0)
i set its orientation at the same time
what i did was i tried changing its orientation when i pressed R only but when i moved the part to other grid it would go back to its old orientation
so scrapped that and then i thought
idk if this will work haven't tried it
so when i rotate
i check if the ray is coliding ig with the itemtospawn
if not
i reposition itemtospawn to the grid raypoint is pointing on
this is the one
and other one was i put all the grids to one model and i get min and max position values of the model if the part is outside these positions i position the part back to its nearest inside position ig
I would probably do this all with code instead of so many instances, but I think that system would probably work.
i would want to do it with all code but ig i don't got enough experience yet is it posible to spawn a grid without using parts
well
grids are just like clamped coordinates
which you can do to the nearest nth degree by doing something like
math.round(5/STEP) * STEP
that would require a lot of math then right
cframe = CFrame.new(grid:GetPivot() + Vector3.new(0,itemToSpawn.PrimaryPart.Size.Y/2,0)) * CFrame.Angles(0, math.rad(Rotation), 0) idk if i did smth wrong but the itemtospawn doesn't move anymore
looks the same?
GetPivot returns a cframe right
yes
posible to use cframe in cframe?
i think you need to do .Position after :GetPivot()
still not moving
nope
was a stupid issue
fixed it
11:02:37.051 Players.Mr_Mordor.PlayerGui.ScreenGui.GameController:181: invalid argument #2 (Vector3 expected, got CFrame) - Client - GameController:181
when i use GetPivot
cframe = CFrame.new(grid:PivotTo() + Vector3.new(0,itemToSpawn.PrimaryPart.Size.Y/2,0)) * CFrame.Angles(0, math.rad(Rotation), 0)
how would i calculate model's max and min size / pos
local min,max = CheckBoundires(gridModel)
function CheckBoundires(Boundary)
local min = Boundary.Position - (Boundary.Size / 2)
local max = Boundary.Position + (Boundary.Size / 2)
return min,max
end
would this work?
sorry if im being annoying
I think it would but you should use cframe for that I think
in case you ever want to rotate your plots
its a model tho
grid is like one block and i just spawn 6x6 grid with these blocks
and model it into one
uh alright then hmm
11:20:29.242 Position is not a valid member of Model "Workspace.House.GridClones" - Client - GameController:153
what could i use for a model?
GridClones is a model made up from Grid blocks
function CheckBoundires(Boundary)
local min = Boundary.Position - (Boundary.Size / 2)
local max = Boundary.Position + (Boundary.Size / 2)
return min,max
end
you could get :GetExtentsSize() or something
it'd work for both i think
go check
i can place here but not here
local min,max = CheckBoundires(gridModel)
local partMin, partMax = CheckBoundires(itemToSpawn)
--canPlace = true
if partMin > min or partMax > max then
canPlace = false
ColorPlaceHolderItem(Color3.new(1,0,0))
else if partMin <= min and partMax <= max then
canPlace = true
ColorPlaceHolderItem(Color3.new(0,1,0))
end
end
function CheckBoundires(Boundary)
local min = Boundary:GetPivot().X - (Boundary:GetExtentsSize().X / 2)
local max = Boundary:GetPivot().Z + (Boundary:GetExtentsSize().Z / 2)
return min,max
end
what would i get
what i dont understand is
i need both
min x
and max x right
as well as z
min z and max z
damn i cant understand this
you here?
** You are now Level 7! **
cant think atm i was gonna ask about how would i know if the model is inside the bounds another model or outside
you could do a get part bounds in radius, get all the corners and see if they're in the range, or do some like vector math to find out
how do i do the first one
how would i get all the corners
yo got a quick question if ur still here
or actually i think ik the answer nvm
@dusky otter sorry for tagging
u here?
basically i fixed the rotation position with this 
else if Rotation == 90 then
cframe = CFrame.new(Vector3.new(currentPivot.X+4,currentPivot.Y,currentPivot.Z)
+
Vector3.new(0,itemToSpawn.PrimaryPart.Size.Y/2,0))
*
CFrame.Angles(0,math.rad(Rotation),0)
else if Rotation == 180 then
cframe = CFrame.new(Vector3.new(currentPivot.X+4,currentPivot.Y,currentPivot.Z-4)
+
Vector3.new(0,itemToSpawn.PrimaryPart.Size.Y/2,0))
*
CFrame.Angles(0,math.rad(Rotation),0)
else if Rotation == 270 then
cframe = CFrame.new(Vector3.new(currentPivot.X,currentPivot.Y,currentPivot.Z-4)
+
Vector3.new(0,itemToSpawn.PrimaryPart.Size.Y/2,0))
*
CFrame.Angles(0,math.rad(Rotation),0)
end
end
end
end
```but its not good do u think i could get the same result any other way
cause this code is bs
@woven hornet
basically
this was the problem
and i fixed it by i checking its rotation and repositioning it like this
but that code is really bad ;d
i was wondering if there's anyway i could get the same results
but not have to write a paragraph
i have to move it by pivot
but i want to rotate it on the spot
basically 1s
this is what i want
huh?
itemToSpawn:PivotTo(cframe)
this is how it moves/rotates
the pivot
is offset to
-2,0,2
yes
its like that
in the corner
and that causes this to happen when i rotate
this
alrt so
what i do is
grid parts are setup like this
but i put pivot
in left corner
i do
gridpart:GetPivot()
which returns its cframe
then i add part.position.y/2
so its not in ground
and when i do
cframe = CFrame.new(currentPivot) * CFrame.Angles(0, math.rad(Rotation),0) this basically
because of its offset
it rotates like this
instead of this
CFrame.new(offset) ? basically add the offset?
wait
lms
parts offset
or grids offset
or it dont matter
how would i get its offset tho
the desk is a model
local offset = (currentPivot.Position-grid.Position).Magnitude
with this
yup
4,0,4
so it just moved 2 studs
it should be the same size as baseplate grid
oh
that yeah
its just
a texture tho
i could make it smaller
could i calculate the
x and z difference
between currentgridpos
and parts position
and add that
same result
grid.size/2 is alrd 2,0,2
i'd still get the same result
it wouldn't be accurate
yes
no
i mean
yes
i removed it
ima try to calculate the x and z difference between them and just try to relocate
cframe = CFrame.new(
Vector3.new(currentPivot.X,currentPivot.Y,currentPivot.Z)
+
Vector3.new(0,itemToSpawn.PrimaryPart.Size.Y/2,0))
*
CFrame.Angles(0, math.rad(Rotation),0) * CFrame.new(2,0,2)
** You are now Level 7! **
wait()
Rotation = (Rotation + 90) % 360
print(Rotation)
this
for rotation
nothing else
uhh y would i use that
for x = startX, startX + (gridWidth - 1) * gridX, gridX do
for z = startZ, startZ + (gridHeight - 1) * gridZ, gridZ do
local gridclone = gridpart:Clone()
gridclone.Parent = workspace.House.Grid
gridclone:PivotTo(CFrame.new(x, 0.5, z))
end
end
i get 1 grid model and just
spawn it
so
i give position
it takes grid part from replicated storage
sizes it as it should be
and then it just teleports to its place
lets say first grid is at pos 5,0,10 next one would be 9,0,14
How would i make a grid from mouseposition
r u talking about this or the whole grid wait
u mean the black grid
or
the white
mb i cant understand what ur saying i've been trying to fix this for the past 2 days
brain aint functioning
local grid = result.Instance
local currentPivot = grid:GetPivot()
same result when rotating
not its off
now the grid is off
its like 2,0,2
no its like
2
2,0,0
** You are now Level 8! **
maybe
current pivot?
what i dont understand is
that would still make the part
rotate
the same way tho no
can u go in a game get a part
put the parts pivotoffset to
left corner
and rotate the part
because of its pivotoffset it rotates differently how it should rotate is like CTRL R
if i also change
the pivot offset on part then lms 1s
same issue
do u know
restoraunt tycoon 2 or smth
game
basically it should move around
like this
and rotate like this
Check out 🤵 Restaurant Tycoon 2. It’s one of the millions of unique, user-generated 3D experiences created on Roblox. 🤵PRESIDENT UPDATE - If your restaurant is 4 stars ⭐ or above, then president customers may visit! Make sure to collect the coins and boxes they drop 😱
🍔Create your own restaurant, cafe, or diner, and cook delicious food for your...
if u check this game
for example
im trying to get similar building
no when i rotate
the part and mouse location are different
it bugs out
i was using pivot but 1s
on the
so on this axis
its not correct
but on
its almost correct
local grid = result.Instance
local currentPivot = grid.CFrame
--local offset = (currentPivot.Position-grid.Position).Magnitude
cframe = CFrame.new(
Vector3.new(currentPivot.X,currentPivot.Y,currentPivot.Z)
+
Vector3.new(0,itemToSpawn.PrimaryPart.Size.Y/2,0))
*
CFrame.Angles(0, math.rad(Rotation),0) * CFrame.new(2,0,-2)
i did this and
itemToSpawn:SetPrimaryPartCFrame(cframe)
that would make it rotate the same way
yup
its cause
uhh
r u in studio
@woven hornet
so
try rotating the part
90 degrees
using its pivot point
thats in left corner
and then try rotating it by pressing ctrl r
yes when i use
PivotTo on part
it does that
should i set its pivotpoint to center as well/
yeah not working
i think im gon have to go with the paragraph script for now and maybe think of smth else
wheres ur itemtospawns
pivot agian
center?
or left
can u show me how it rotates on ur thing
on my thing its not even moving anymore
it moved now
but
not quite there
no? i said its in the left
should i center it?
uhh what?
idk
local grid = result.Instance
local currentPivot = grid.Position
cframe = CFrame.new(Vector3.new(currentPivot.X,currentPivot.Y,currentPivot.Z) + Vector3.new(0,itemToSpawn.PrimaryPart.Size.Y/2,0)) * CFrame.Angles(0, math.rad(Rotation),0) * CFrame.new(2,0,2)
this is the script
i get what ur asking now
its off by 1
its just on the right grid
and on the other one its on the left grid
when i do just
currentpivot
i get a error
yes
offset
still buggin
cframe = CFrame.new(currentPivot + Vector3.new(0,itemToSpawn.PrimaryPart.Size.Y/2,0))
oh shit
it works
cframe = CFrame.new(currentPivot + Vector3.new(0,itemToSpawn.PrimaryPart.Size.Y/2,0)) * CFrame.Angles(0, math.rad(Rotation),0) * CFrame.new(-2,0,2)
i did this
and it worked
but wait 1s
yeah ik
i've
been trying both
but now it worked
idk wtf i was doing tbh
but it works
:D
thanks bro
yeah
i watched
some
unity grid tutorial
the guy did it like that there
so i was like let me do that
first time i've ever tried placement system so lot to learn ngl
dino i got one question if ur still here
u got any idea how i could detect if the part is going outside the grid boundires?
basically for this to not happen
ill give it a try thanks