Hello Developers,
I am currently working on a system that allows players to move a Model
. With that system, I want to make it so players can rotate said Model
.
For moving the Model, I am using Model:PivotTo()
. But the issue comes when I rotate my Model and move my mouse. When rotating the Model, I am not taking in account of the rotation. So, it “Resets” the rotation applied.
Problem solved, right?- Nope.
Because I’m working with stuff which I don’t fully understand. (Cough Math Cough)
TLDR:
My Model’s rotation is being reset when I move my mouse.
Code
(Local Script)
-- // Services // --
local Players = game:GetService("Players")
local UserInputService = game:GetService("UserInputService")
-- // Variables // --
local Booth = workspace:WaitForChild("TheGenericBooth")
local ZeroBuildZone = workspace:WaitForChild("ZeroBuildZone")
local maxDistance = 1000
local Mouse = Players.LocalPlayer:GetMouse()
-- // Functions // --
local function getWorldMousePosition()
-- Params --
local params = RaycastParams.new()
params.FilterType = Enum.RaycastFilterType.Exclude
params.FilterDescendantsInstances = { Players.LocalPlayer.Character, workspace.TheGenericBooth }
-- Raycast --
local mouseLocation = UserInputService:GetMouseLocation()
local screenToWorldRay = workspace.CurrentCamera:ViewportPointToRay(mouseLocation.X, mouseLocation.Y)
local directionVector = screenToWorldRay.Direction * maxDistance
local raycastResult = workspace:Raycast(screenToWorldRay.Origin, directionVector, params)
-- Return --
return raycastResult
end
-- // Mouse // --
Mouse.Move:Connect(function()
local Cast = getWorldMousePosition()
local Success, Error = pcall(function()
local NewPivot = CFrame.new(Cast.Position) + (Booth.PrimaryPart.Size/2 * Cast.Normal)
Booth:PivotTo(NewPivot)
end)
if not Success then
print(Error)
end
end)
Mouse.Button1Down:Connect(function()
-- Variables --
local BoundingBox = ZeroBuildZone:GetBoundingBox()
local Region = Region3.new(ZeroBuildZone.PrimaryPart.Position - 0.5 * ZeroBuildZone.PrimaryPart.Size, ZeroBuildZone.PrimaryPart.Position + 0.5 * ZeroBuildZone.PrimaryPart.Size)
local PartsInRegion = workspace:GetPartBoundsInBox(ZeroBuildZone.PrimaryPart.CFrame, ZeroBuildZone.PrimaryPart.Size)
-- Print Parts in Zero Build Zone --
for a, Part: BasePart in pairs(PartsInRegion) do
if Part.Name == "BoothBorder" then
print(Part.Name)
end
end
end)
-- // Rotation // --
local function RotateBooth()
-- Variables --
local rotation = CFrame.Angles(0, math.rad(90), 0)
local modelCFrame = Booth:GetPivot()
Booth:PivotTo(modelCFrame * rotation)
end
UserInputService.InputBegan:Connect(function(Input: InputObject, GPE: boolean)
if Input.UserInputType == Enum.UserInputType.Keyboard then
if Input.KeyCode == Enum.KeyCode.R or Input.KeyCode == Enum.KeyCode.Right then
RotateBooth()
end
end
end)