How do i get objects to rotate when i rotate my camera? (Object grabbing system)

So i have this grabbing system, and currently when i grab an object, its orientation stays the same. I need it so the object rotates when your camera rotates the will also rotate.

Here’s an example of what im trying to achieve

How would i achieve this?

local Players = game:GetService("Players")
local Player = Players.LocalPlayer
repeat wait() until Player.Character
local Char = Player.Character
local Humanoid = Char:WaitForChild("Humanoid")
local HRP = Char:WaitForChild("HumanoidRootPart")
local PlayerGui = Player:WaitForChild("PlayerGui")
local GrabGui = PlayerGui:WaitForChild("GrabGui")
local UIS = game:GetService("UserInputService")
local RS = game:GetService("RunService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Mouse = Player:GetMouse() 
local Camera = workspace.CurrentCamera 

local SetNetworkOwnerEvent = ReplicatedStorage:WaitForChild("SetNetworkOwnerEvent")
local GrabEvent = ReplicatedStorage:WaitForChild("GrabEvent")

local Holding = false
local MaxGrabDistance = 10

local Object
local GrabbingForce
local GrabbingGyro

local ObjectDistance

local Animation = script:WaitForChild("HoldAnimation")
local AnimTrack = nil

function PlayAnimation(PlayAnim)
	
	if PlayAnim and not AnimTrack then
		AnimTrack = Humanoid:LoadAnimation(Animation)
		AnimTrack:Play()
	elseif AnimTrack and not PlayAnim then
		AnimTrack:Stop()
		AnimTrack = nil
	end
end

-- Functions
function Grab()
	print("Grabbing")
	local BodyPos = Instance.new("BodyPosition")
	local BodyGyro = Instance.new("BodyGyro")
	BodyPos.D = 650
	BodyGyro.D = 650
	BodyGyro.MaxTorque = Vector3.new(2000, 2000, 2000)
	Object = Mouse.Target
	SetNetworkOwnerEvent:FireServer(Object, true)
	local CanGrab = Object.CanGrab
	GrabEvent:FireServer(CanGrab, false)
	CanGrab.Value = false
		
	GrabbingForce = BodyPos
	GrabbingGyro = BodyGyro
	
	GrabbingForce.Name = "GrabbingForce"
	GrabbingForce.Parent = Object

	GrabbingGyro.Name = "GrabbingGyro"
	GrabbingGyro.Parent = Object
	
	ObjectDistance = (Mouse.Target.Position - HRP.Position).Magnitude
	
	PlayAnimation(true)
	
	Holding = true
end

function Release()
	if Holding == true then
		print("Releasing")
		GrabbingForce:Destroy()
		GrabbingGyro:Destroy()
		SetNetworkOwnerEvent:FireServer(Object, false)
		GrabEvent:FireServer(Object:FindFirstChild("CanGrab"), true)
		local CanGrab = Object.CanGrab
		CanGrab.Value = true
		Object = nil
		
		PlayAnimation(false)
				
		Holding = false
	end
end

-- Start Grab
UIS.InputBegan:Connect(function(inputObject) 
    if (inputObject.UserInputType == Enum.UserInputType.MouseButton1 or inputObject.KeyCode == Enum.KeyCode.ButtonR2) and Mouse.Target and not Mouse.Target.Locked and Humanoid.Health > 0 then
		local magnitude = (Mouse.Target.Position - HRP.Position).Magnitude
		local CanGrabValue = Mouse.Target:FindFirstChild("CanGrab")
		if Mouse.Target and not Mouse.Target.Locked and magnitude < MaxGrabDistance and not Holding and CanGrabValue and CanGrabValue.Value == true then
			Grab()
		else
			Release()
		end
	end
end)

-- End Grab
UIS.InputEnded:Connect(function(inputObject)
    if inputObject.UserInputType == Enum.UserInputType.MouseButton1 or inputObject.KeyCode == Enum.KeyCode.ButtonR2 then
		Release()
	end
end)

RS.RenderStepped:Connect(function()
	
	-- Grab GUI
	if Mouse.Target and not Mouse.Target.Locked then
		local magnitude = (Mouse.Target.Position - HRP.Position).Magnitude
		if magnitude < MaxGrabDistance and not Holding then
			local CanGrabValue = Mouse.Target:FindFirstChild("CanGrab")
			if CanGrabValue and CanGrabValue.Value == true then
				GrabGui.GrabText.Visible = true
			else
				GrabGui.GrabText.Visible = false
			end
		else
			GrabGui.GrabText.Visible = false
		end
	else
		GrabGui.GrabText.Visible = false
	end
	
	-- Grabbing
	if Holding and Object and Object:IsA("BasePart") then
		local magnitude = (Object.Position - HRP.Position).Magnitude
		if Holding and Object and GrabbingForce and GrabbingGyro and magnitude < (MaxGrabDistance + 5) then
			GrabbingForce.Position = Camera.CFrame.Position + (Mouse.UnitRay.Direction * (ObjectDistance + (workspace.CurrentCamera.CoordinateFrame.p - HRP.Position + Vector3.new(0, 1, 0)).magnitude - 2))
			GrabbingGyro.CFrame = Object.CFrame
		else
			Release()
		end
		-- Anti fly
		if Object and Object.Parent then
			Object.Touched:Connect(function(hit)
				if hit.Parent and hit.Parent:FindFirstChild("Humanoid") and hit.Parent == Char and hit == Object then
					Release()
				end
			end)
		end
	end
end)

-- Make sure that theres no floating object when the player leaves or dies
Players.PlayerRemoving:Connect(function(player)
	if Object and Object.Parent and player == Player then
		Release()
	end
end)

Humanoid.Died:Connect(function()
	if Object and Object.Parent then
		Release()
	end
end)
2 Likes

Hey! Roblox has a cool little thing called a “AlignOrienatation”. I think this should provide a solution to your issue. It essentially does what it says on the tin, alligns orientation.

It’s a constraint that applies a torque to make its attachments align. Like other constraints, this has two Attachments. The position doesn’t even need to match!

You will need to insert an attachment into the camera and the grabbed object. Then link both of these to the newly inserted AllignOrientation using Attachment1 and Attachment0. You can fiddle with things such as “Primary axis” and “Torque magnitude” to try and make the result the best.

Hopefully this works with cameras and you can sort your issue out. If you have any issues arise with this, or it doesn’t work, please let me know and I can try help you.

If you want some more info about AllignOrientation then take a look at this link:

Have a great day! :smile:

2 Likes

This works, but when you grab the object, its orientation just goes with your character’s orientation. Example; say my character’s orientation is (25, 0, 0) and the object i grab is like (0, 0 -45) the object will automatically go to (25, 0, 0) making it impossible to change the direction of the object being grabbed. I also can’t rotate the object up and down, since my character can’t move up and down. I would try to go off the camera, but i cant use a constraint on the camera.

You can use ToObjectSpace() to get the rotation of an object relative to the camera and set the gyro’s CFrame to it.

How do i use it? When i attempt to use it, it just doesn’t end up well. I don’t know a whole lot about CFrames.

Do you need to grabbing to be in sync with all the other clients or only the player that grabbed it.

Physics always replicate. Even if you are managing from the client.

No its does not, it only does if your doing it on the character or you set the network owner of the object to the player. For example if you apply a body mover to a part in the workspace on the client only that client will see it not others.

Have you even tried yourself? Any unachored objects being moved by body movers from the client, will automatically replicate to the server.


This is servers view when i grab objects.

1 Like

You literally have a setnetworkowner event lol thats why its working, try removing that and see it it replicates. I am assuming that event sets the network owner of that part on the server to the player.

That doesn’t make it replicate. Do you even know what setnetworkownership is? if you don’t here’s an article: Network Ownership | Documentation - Roblox Creator Hub

1 Like

“Any changes to an object the client has network ownership over will replicate to the server. Clients have network ownership of their characters by default so any physics changes you make, including through body movers, will replicate.” I don’t even need events in my code. The server automatically manages network Ownership

no, it is not. The part is in the workspace.

I get what you are trying to say… but any links on how to properly use attachments? Or where would you attach attachments into the camera?

You can read about attachments & constraints and how they work here:
Attachments & Constraints

Here are some links to useful API references:
Attachment
AllignOrientation
AllignPosition

Let me know if you need more specific help!

1 Like