Turning a welded part to face another part

  1. What do you want to achieve? The turret on the right should point at the turret on the left

  2. What is the issue? It points in the correct direction but it moves to the center of the world

    The code

local shipBody = script.Parent.Part
local target =  workspace.Ships.Team1.Enemy1.Head

local CF = CFrame.new(shipBody.Weld.C1.Position,target.Position)
x,y,z = CF:ToEulerAnglesXYZ()

shipBody.Weld.C0 = shipBody.CFrame:ToObjectSpace(CFrame.Angles(x,y,z))
  1. What solutions have you tried so far? I’ve tried doing things like
shipBody.Weld.C0 = shipBody.CFrame:ToObjectSpace(shipBody.Weld.Part1.CFrame * CFrame.Angles(x,y,z))

which makes it so that it’s in the right spot but it doesn’t face the right direction.
I’ve tried other variations like using C0 and C1 instead of part1.Cframe and those all end up in the middle of the world

I can’t use CFrame.new(position, TargetPosition) to make it look because the “ship” is going to be able to move and that results in this problem

I highly recommend using WeldConstraints to weld the turrets since Weld is quite old. Only difference is, is that WeldConstraints don’t have any CFrame you can adjust to which should make it a lot easier.

Note: Name what was Part1 inside the weld to whatever you’d like. i’ll just call it Part1

local CF = CFrame.new(Part1.Position,target.Position)

x,y,z = CF:ToEulerAnglesXYZ()

shipBody.CFrame = shipBody.CFrame:ToObjectSpace(shipBody.CFrame * CFrame.Angles(x,y,z))

Is it possible to rotate a WeldConstrainted part without rotating the whole model?
Also I tried out your code but maybe I did it wrong cause it still went to the middle of the world and the whole thing turns instead of just the turret which I don’t want.

Can you show me how your turret is setup in the explorer and yes it is possible


So the whole point of this thread was to fix this problem where the turrets would stay behind while the “ship” moved

Me in my infinite wisdom assumed it was something to do with how the code used CFrame to turn the turret to face the target. This was not the case.

turrets.C0 = shipBody.CFrame:ToObjectSpace(CFrame.new(turrets.Part1.Position, target.Head.Position))

I ended up basically remaking the same exact code just more complicated to try and solve it.

local CF = CFrame.new(turrets.Part1.Position,target.Head.Position)
x,y,z = CF:ToEulerAnglesXYZ()
local pos = turrets.Part1.Position
turrets.C0 = shipBody.CFrame:ToObjectSpace(CFrame.new(pos) * CFrame.Angles(x,y,z))

The real problem was with how I was moving the ship using tween service. I found out after messing around that when using tween service the parts welded to the main block of the ship wouldn’t move with it on the server but they would on the client leading to the behavior seen in the first video.

So the solution ended up being changing how I moved the ship instead of using tweenservice to using CFrame to move it.

This probably isn’t the best solution in terms of performance but it’s the best I could figure out. On the bright side this all taught me a lot more about CFrame.