How would I Move a Model, but also Change it's Rotation?

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

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)
2 Likes

So… you get the bounding box from the model, offset it with the mouse position, and then offset it with cframe.angles() and the whole thing would be like:

Model:GetBoundingBox().Position*CFrame.new(MousePosition)*CFrame.angles(math.rad(45),0,0) -- Offsets the rotation by 45° on the X axis
1 Like

Unfortunately I got the error

invalid argument #1 (CFrame expected, got Vector3)

Which I believe it’s because I use Cast.Position where you said MousePosition.

1 Like

My bad! I put Model:GetBoundingBox().Position expecting it would output the cframe without the rotation but it outputted a vector3, what you can do is replace Model:GetBoundingBox().Position with CFrame.new(Model:GetBoundingBox().Position)

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.