So basically i’m working on a “Animated” Button for future use.
But i seemed to run into a problem, when the Model is placed in a “normal” position the button seems to work fine. But when the Model is rotated in a “unusual” position and the button is pressed the primary part changes to a “normal” position and not to the rotated one.
How it works in a “normal” position:
How it works in a “unusual” position (Rotated by 90 Degress to the right and tilted by 30 Degress:
I tried to rotate the model via script and such but it didn’t proven successful.
for i = 1,1 do wait() Button:SetPrimaryPartCFrame(CFrame.new(Button.PrimaryPart.Position + Vector3.new(0,0.04,0))) end
Thanks in advance, and i’m probably overlooking/forgot something.
You have to use look vector:
for i = 1,1 do wait()
Button:SetPrimaryPartCFrame(CFrame.new(Button.PrimaryPart.CFrame * Button.PrimaryPart.CFrame.lookVector)
Let’s take a small step back so we can understand what’s going wrong here.
Face it, vectors are boring. Vectors are always relative to world space, meaning the axes never change. When you add
PrimaryPart.Position (a vector) and
Vector3.new(0, 0.04, 0) together, your resulting vector is moved up 0.04 studs on the Y axis, relative to world space. It doesn’t take rotation into account.
So how do you do this? How do you make vectors have a position and a rotation? How could I make the button move relative to its parent?
The answer lies in
Coordinate Frame matrix multiplication, basically CFrame arithmetic. CFrame is a datatype whose sole purpose is to be a vector with a rotational counterpart, making things like this possible without any understanding of trigonometry whatsoever. When you multiply two CFrames (using the multiplication operator:
*) you can easily traverse the lands of Robloxia in a way no man ever could. Multiplying two CFrames will move relative to the first, taking any rotation into account, which is exactly what we want!
Anyways, enough explaining. have some code:
for i = 1, 1 do
Button:SetPrimaryPartCFrame( Button.PrimaryPart.CFrame * CFrame.new(0, 0.04, 0) )
Alright, makes sense imma be playing around with it a bit to learn it. Thanks!