Trying to apply CFrame rotation in only one axis on a welded basepart

The following code prints:
0, -15, 90 | 0, 0, 0
0, 0, 0 | 0, 0, 0

but the turret is not rotating at all.

while true do
	if target.Value ~= nil then
		local v = CFrame.new(turret.Position, target.Value.Position):VectorToObjectSpace()
		print(turret.Orientation, v)
		turret.Orientation = v
		print(turret.Orientation, v)
	end
	wait(1/20)
end

What am I doing wrong?

It takes a Vector3 argument.

1 Like

Sorry, i’m complexly lost in CFrames. They use a different rotational system than the ‘Rotation’ option on Baseparts? How do I pick out one axis from the rotation of a CFrame and apply it to a Basepart?

**from a CFrame made from two points

If you’re wanting to change the orientation of the turret, it would be best to alter its CFrame, not orientation

I’m not a CFrame expert, so this comes from my own tinkering.

If you want to rotate about a CFrame’s up/look/right vectors, you can use CFrame.Angles:

local CF = CFrame.new() -- the starting CFrame

RunService.Heartbeat:Connect(function()
    Part.CFrame = CF * CFrame.Angles(0, math.pi * tick() % (2 * math.pi), 0)
end)

That would look like this


If you want to rotate about an arbitrary axis, you can use CFrame.fromAxisAngle:

local CF = CFrame.new() -- the starting CFrame
local Axis = Vector3.new() -- the axis to rotate about

RunService.Heartbeat:Connect(function()
	Part.CFrame =
		CFrame.fromAxisAngle(
			Axis,
			math.pi * tick() % (2 * math.pi)
		):Inverse()
		* (CF - CF.Position)
		+ CF.Position
end)

I wish I could tell you why this works, but like I said, I figured this out through experimentation. There is probably a better solution.

3 Likes

Well I have tried multiplying by CFrame.angles before. Might it have anything to do with the part being welded to a larger model?

If it’s welded then you should be modifying the C0/C1 properties and not the CFrame directly.

Here, this is what I’m trying to do but now i’m having trouble converting values, and it’s still being glitchy as well.

while true do
	if target.Value ~= nil then
		local n = CFrame.new(turret.Position, target.Value.Position):()
		local weld = turret:FindFirstChildWhichIsA('Weld')
		weld.C1 = weld.C1 * CFrame.fromAxisAngle(0, n.Y, 0)
		break
	end
	wait(1/20)
end

The break is for debugging.

How do I modify them if they are Userdatas?

I’m not sure what you’re trying to do here. On the third line, you have a :() that doesn’t do anything. On the fifth line, I don’t think you’re using fromAxisAngle correctly. Also, what do you mean that it’s being glitchy?

Anyway, this post piqued my interest so I had a go at it myself.

Turret.rbxl (31.5 KB)

12 Likes