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

Buggy Rotation? Grid Placement System.

Asked by 4 years ago

Hello,

I have recently been making a Grid placement system for my game everything is going well except for the rotation.

https://gyazo.com/ee0466084ef50e1ca7a79a84df355b63

Issue 1: When I rotate when the mouse isn't moving, its really buggy. But when it is moving it's fine.

Issue 2: When the object is placed, the placed object isn't rotated.

Code:

local mouse = game.Players.LocalPlayer:GetMouse()
local Model = workspace:WaitForChild('Window1')
local posX = mouse.Hit.X
local posY = mouse.Hit.Y
local posZ = mouse.Hit.Z
local gridSize = 1
local Rot = 0 

local function rotate()
    Rot = Rot + 45
    Model:SetPrimaryPartCFrame(Model.PrimaryPart.CFrame * CFrame.Angles(0, math.rad(Rot), 0))
end

mouse.KeyDown:connect(function(key)
    if key == "r" then
    rotate()
    end
end)

local function Snap()
    posX = math.floor(mouse.Hit.X / gridSize + 0.5) * gridSize
    posY = mouse.Hit.Y
    posZ = math.floor(mouse.Hit.Z / gridSize + 0.5) * gridSize
end

mouse.Move:Connect(function()
    Snap()
    Model:SetPrimaryPartCFrame(CFrame.new(posX, posY + 3.3, posZ))
    Model:SetPrimaryPartCFrame(Model.PrimaryPart.CFrame * CFrame.Angles(0, math.rad(Rot), 0))
    mouse.TargetFilter = Model
end)

local function Place()
    local WindowCopy = game:GetService('ReplicatedStorage').Objects.Window1:Clone()
    WindowCopy.Parent = workspace
    WindowCopy:SetPrimaryPartCFrame(CFrame.new(posX, posY + 3.3, posZ))
    mouse.TargetFilter = WindowCopy
end

mouse.Button1Down:Connect(function()
    Place()
end)

Thank You.

2 answers

Log in to vote
0
Answered by
ScuffedAI 435 Moderation Voter
4 years ago
Edited 4 years ago

Regarding issue 2: the object doesn't get rotated because on line 36, you only provide the position of the object and not its orientation.

Edit:

To provide the orientation you would simply add CFrame Angles to the CFrame. Like such:

-- I created a variable for each CFrame to make it easier to read.

local position = CFrame.new(posX, posY + 3.3, posZ)
local orientation = CFrame.Angles(0, math.rad(Rot), 0))

WindowCopy:SetPrimaryPartCFrame(position *  orientation)
Ad
Log in to vote
0
Answered by 4 years ago

How would I provide the rotation?

Answer this question