How would I make a draggable model in a Viewport Frame?

I am trying to make a draggable model in a Viewport Frame, like in Phantom Forces where you can inspect a weapon by dragging it. I am not the best at CFrame or Mouse positioning. But if you could help in any way it would be nice! :smiley:

You’re referencing the Weapon Loadout screen from Phantom Forces right? I don’t think that example actually uses a ViewportFrame because (unless this has been updated) ViewportFrames cannot render Decals or Texutres. Either way the procedure would be roughly the same: You will want to cover the area where the Model will be positioned relative to the Camera with a Frame, or if using a ViewportFrame just position it where you want the Model to be rendered. Then you will need to bind InputBegan, InputChanged, and InputEnded events to the Frame or ViewportFrame. When the button you want to trigger the start of a drag is pressed or released change a global variable isDragging or something like that to true or false respectively in InputBegan and InputEnded. Also store the intial position of the mouse InputObject.Position when the trigger button is pressed in InputBegan. Using RunService:BindToRenderStep() connect a function that will use :SetPrimaryPartCFrame() to position the model relative to the Camera (Note: if you’re not using a Scriptable CameraType then the default camera scripts will be buggy due to raycasting into the Model if its parts have CanCollide property set to true). In either the InputChanged or the function bound to render step, determine if the mouse is being dragged (isDragging) and then calculate to offset from its initial drag position. Use this offset to determine how much you would like the Model to be rotated and update its CFrame.

Hope that’s not too abstract! I might be able to write up a quick example if needed.

3 Likes

DraggableViewportFrame2 (3)_AutoRecovery_0.rbxl (102.1 KB)

4 Likes