Rotating a frame based on a pivot

I’m trying to script a needle for a speedometer, and I’m having trouble with rotating it. When I rotate it, it rotates at the center, causing both the end of the needle and the base of the frame to rotate, when I only want the needle to rotate. How should I go about fixing this?

1 Like

Is the needle a ui frame?
If so, you can create an empty frame, and set it’s background transparency to 1.
Then, you put the needle ui inside the empty frame.
You then set the needle’s position to {0, 0},{-0.5, 0}, and it’s size to {1, 0},{1, 0}, supposing the needle faces upwards.
Then you can rotate the needle, like it has a pivot.

I haven’t tested this, so I might be wrong.

1 Like

You can use the pivot system. Set the pivot to the rotation origin, and then just use model:PivotTo().

Alternatively, using CFrames, you store the original offset (that is to say, needle.Position - base.Position). Then, to calculate the needle’s CFrame at a specific angle, you do some math with the offset.

local offset= needle.Position - base.Position
local offsetCFrame = CFrame.new(offset)

You’ll need to add a rotation, which can take a Vector3 as input, but as you want to lock it to 1 axis (i assume) you can also just make it a number.


local rot = Vector3.new(0,0,math.rad(angle))
local rotCFrame = CFrame.fromEulerAnglesXYZ(rot) 

And then, with some math and these values, we can rotate around the point. Note that this is a global rotation around the axis; setting the rotation to X should result in the same CFrame (instead of incrementing).

local newCFrame = CFrame.new(base.Position+originalOffset) * offsetCFrame:Inverse() * rotCFrame * offsetCFrame

You’ll need to store offset, so calculate it at the start only.

local offset= needle.Position - base.Position
local offsetCFrame = CFrame.new(offset)

function rotate(rot)
local rotCFrame = CFrame.fromEulerAnglesXYZ(rot) 

local newCFrame = CFrame.new(base.Position+originalOffset) * offsetCFrame:Inverse() * rotCFrame * offsetCFrame
return newCFrame
end

Video: https://gyazo.com/2c65afaf31a420c93d1d82c4279f8a0f

image

You just need to put a frame inside another and rotate the top one.

image

2 Likes

I think I just spent 10 minutes explaining something different, having misread the question. Whoops

1 Like