#i need help with rotating a part (making a grid placement system)

1 messages · Page 1 of 1 (latest)

limber veldt
#

basically what happends is ill explain with pictures

#

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

limber veldt
#

@dusky otter

dusky otter
#

thank

limber veldt
#

no no thank you

#

my brain hurts from trying to fix this

dusky otter
#

lol i know the feeling

#

alright let's talk about what it is ur trying to do lol

limber veldt
#

alrt so

#

im making a game and i need placement system for it

#

first i made placement system

#

similar to tower defense games

dusky otter
#

okay

limber veldt
#

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...

▶ Play video
#

if u go to

#

0:50

#

thats how parts are setup

#

as well as the grid parts

dusky otter
#

okay makes sense

#

so you're just having trouble with the rotation or what?

limber veldt
#

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

limber veldt
#

the red drawing here

#

is how i want it to be

#

without going ot of bounds

limber veldt
#

how it is

dusky otter
#

oh interesting

limber veldt
#

i can send the full script if u want but i just sent the parts that handle rotations

dusky otter
#

can I see that code again

limber veldt
#

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)
dusky otter
#

okay yeah

limber veldt
#

i have lots of comments cause i was trying diff stuff

dusky otter
#

you ever heard of a guard?

limber veldt
#

guard?

#

nope

dusky otter
#

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

limber veldt
#

yes

dusky otter
#

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?

limber veldt
#

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

dusky otter
#

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

limber veldt
#

yup take all the time u need ill send a vid

dusky otter
#

that should be fine I think then

#

yeah a vid would help

#

please lol

limber veldt
#

that could help 1s

#

20 seconds

#

its uploading xd

#

it should process in like

#

15 seconds maybe

dusky otter
#

you should try sharex

#

you can just upload the video file to discord

limber veldt
#

oh fr

dusky otter
#

you should just try share x :)

limber veldt
#

ill get it

dusky otter
limber veldt
#

do u not need nitro

#

to send big vid tho

dusky otter
#

not if its small enough

limber veldt
#

its in 360p but it should be clear enough hopefylly

#

yt taking long to process

#

alrt its in

#

hd now

dusky otter
#

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?

limber veldt
#

this is the desks pivotoffset

#

and this is the grid

dusky otter
#

uh

#

so the desks pivot offset is in the corner?

limber veldt
#

yes

sour ravenBOT
#

studio** You are now Level 6! **studio

limber veldt
#

first it was in the center

#

and i was having another issue

#

want me to show it?

dusky otter
#

if its in the corner wouldn't it only rotate like

limber veldt
#

i have issue with rotation and clipping

dusky otter
#

wouldn't you want it to rotate more like this?

limber veldt
#

thats exactly

#

what i want

dusky otter
#

so why don't we move the pivot to the center right or something

limber veldt
#

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

dusky otter
#

okay

#

thats because you use the pivot of the corner on the grid tile

#

lets try to align those maybe

#

?

limber veldt
#

alrt 1s

#

now it does this

#

not even on the same grid

#

that mouse is on

dusky otter
#

what did u change

limber veldt
dusky otter
#

i dont think you're even using that actually

#

in ur code do :GetPviot() instead of .CFrame

limber veldt
#

damn didnt know that was a thing what i did is i put

#

a small part

#

at where the pivot

#

should have been

#

💀

dusky otter
#

lol

limber veldt
#

should i put the

#

pivot to the same place?

dusky otter
#

play around with it i guess?

limber veldt
#

alrt ill do that how would i fix clipping tho

#

i think that script isn't right atm

dusky otter
#

right

limber veldt
#

here's what i was thinking like 2 ways

dusky otter
#

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

limber veldt
#

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

dusky otter
#

I would probably do this all with code instead of so many instances, but I think that system would probably work.

limber veldt
dusky otter
#

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

limber veldt
#

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

dusky otter
#

looks the same?

limber veldt
#

GetPivot returns a cframe right

dusky otter
#

yes

limber veldt
#

posible to use cframe in cframe?

dusky otter
#

i think you need to do .Position after :GetPivot()

limber veldt
#

oh alrt

#

nope

#

still nothing

dusky otter
#

wdym nothing

#

are you getting any errors?

limber veldt
#

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

dusky otter
#

uh

#

whats on that line

limber veldt
#

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

dusky otter
#

I think it would but you should use cframe for that I think

#

in case you ever want to rotate your plots

limber veldt
#

its a model tho

#

grid is like one block and i just spawn 6x6 grid with these blocks

#

and model it into one

dusky otter
#

uh alright then hmm

limber veldt
#

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

dusky otter
#

you could get :GetExtentsSize() or something

limber veldt
#

lms

#

would that work

#

for position

#

or for size only

dusky otter
#

it'd work for both i think

#

go check

limber veldt
#

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

dusky otter
#

pretty sure orientation is sent first

#

so getting x and z would be bad

limber veldt
#

what would i get

dusky otter
#

oh nvm

#

ur using extents size

#

this would give u something a little different

limber veldt
#

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?

dusky otter
#

sorry I had to use the bathroom

#

wassap

sour ravenBOT
#

studio** You are now Level 7! **studio

limber veldt
#

Sorry saw this late

#

been scripting all day

#

brain is dead

limber veldt
dusky otter
#

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

limber veldt
#

how would i get all the corners

limber veldt
#

or actually i think ik the answer nvm

limber veldt
#

@dusky otter sorry for tagging

#

u here?

#

basically i fixed the rotation position with this agony

                    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

limber veldt
#

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

limber veldt
#

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)

sour ravenBOT
#

studio** You are now Level 7! **studio

limber veldt
#

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

sour ravenBOT
#

studio** You are now Level 8! **studio

limber veldt
#

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

#

like this

#

and rotate like this

#

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