How can I connect a rotating part to a non rotating object?

So I have a barrel that I want to rotate around a moving object, I have the code for the moving object, and the rotating barrel still needs some work, but what I’m having trouble with is having the barrel stick to the base.

Heres an image of what I’m talking about:
a

The red arrow shows that the whole character should move (which it does), and the blue arrow shows that the barrel should rotate around said object.

I have a part to which the barrel is attached to placed in the exact middle of the object. This part rotates to where the player’s mouse is hovering. What I am having trouble with now is making the rotating part, stick to the object. If I use a weld, the rotating part won’t rotate but instead messes with the object’s movement as it tries to turn. if I move the rotating part to the object’s CFrame every heartbeat, the rotating part lags behind the object slightly.

So here’s my question: What could I use/do to attach the rotating part to the Object?

If you want to help with my problem with the rotating part's orientation, expand this.

My Problem with the rotating part is that even when I limit my CFrame.LooKAt() to only the X and Z and make Y = 0, the rotating part still will aim a little up or a little down.

Here’s an image and my code:


Blue is what the orientation should be while red is what it currently is.

ServerScript

local MousePosEvent = script.Parent.Parent.MousePosEvent

MousePosEvent.OnServerEvent:Connect(function(player, mousePosX, mousePosY, mousePosZ)
	script.Parent.CFrame = CFrame.lookAt(script.Parent.Position, Vector3.new(mousePosX, 0, mousePosZ))
end)

LocalScript

local RunService = game:GetService("RunService")
local Bot = script.Parent.Parent:WaitForChild("Bot")
local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()

RunService.RenderStepped:Connect(function()
	local BotObject = Bot.Value
	local RE = Bot.Value:WaitForChild("MousePosEvent")
	
	RE:FireServer(Mouse.Hit.X, Mouse.Hit.Y, Mouse.Hit.Z)
end)

Edit:

Latest progress: How can I connect a rotating part to a non rotating object? - #3 by MRBOGO_YT

1 Like

Have you tried a Hinge Constraint between them?

I’m using a hinge constraint now, but now I have a very weird issue. My hinge constraint only works on the server and not the client. What I mean is that the rotation part rotates normally on the server, but the player cannot see it on the client, it’s just sitting there on the client.

I’ll attach some photos:

Client:

Server:

Here’s the code for the hinge constraint:

ServerScript

local MousePosEvent = script.Parent.Parent.MousePosEvent

MousePosEvent.OnServerEvent:Connect(function(player, mouseLookVector)
	print(mouseLookVector)
	local Point = CFrame.new(script.Parent.Position, mouseLookVector + script.Parent.Position)
	local X, Y, Z = Point:ToOrientation()
	
	script.Parent.HingeConstraint.TargetAngle = math.deg(Y) - script.Parent.Parent.PrimaryPart.Orientation.Y
end)

Have you tried using Pivot Points?


image
(pivot is at the center of the base)

local UserInputService = game:GetService("UserInputService")
local PlayersService = game:GetService("Players")
local Barrel = workspace:WaitForChild("Barrel")

local LocalPlayer = PlayersService.LocalPlayer
local Mouse = LocalPlayer:GetMouse()

local initialPosition = Barrel.Position + Barrel.PivotOffset.Position

UserInputService.InputChanged:Connect(function(input, gameProcessedEvent)
	if gameProcessedEvent then
		return
	end
	
	if input.UserInputType == Enum.UserInputType.MouseMovement then
		local mouseHit = Mouse.Hit
		local facing = CFrame.lookAt(initialPosition, mouseHit.Position)
		local x, y, z = facing:ToOrientation()
		facing = CFrame.Angles(0, y - math.pi/2, z) -- (y - math.pi/2) is there so the cylinder actually faces towards the mouse!
		
		Barrel:PivotTo(CFrame.new(initialPosition) * facing)
	end
end)

I wasn’t sure if you were strictly using constraints though, but you could apply this logic to a Weld as well.

1 Like