I'm attempting to rotate a part about certain surfaces, TopSurface, RightSurface, etc. I have somewhat completed this but the positions do not seem to work properly when the part starts at different orientations.
I do know why some of the problems I have with my code, provided bellow, are happening, I just do not have an idea on what I should be using to fix this and why (Have had a hard time finding good CFrame/Vector3 tutorials that explain in-depth). Bellow my script's source is a break down of what I'm doing.
Script:
function Rotate(Part)
local RotationPoint
local Angles
local function getVectorUnit(P)
if P:FindFirstChild("HingeSurface") then
if P.HingeSurface.Value == "TopSurface" then
RotationPoint = Part.Position + Vector3.new(0, Part.Size.Y/2, 0)
Angles = CFrame.fromAxisAngle(Part.CFrame.rightVector, math.pi/4)
elseif P.HingeSurface.Value == "BottomSurface" then
RotationPoint = Part.Position - Vector3.new(0, 0, Part.Size.Z/2)
Angles = CFrame.fromAxisAngle(Part.CFrame.rightVector * -1, math.pi/4)
elseif P.HingeSurface.Value == "RightSurface" then
RotationPoint = Part.Position + Vector3.new(Part.Size.X/2, 0, 0)
Angles = CFrame.fromAxisAngle(Part.CFrame.lookVector, math.pi/4)
elseif P.HingeSurface.Value == "LeftSurface" then
RotationPoint = Part.Position - Vector3.new(Part.Size.X/2, 0, 0)
Angles = CFrame.fromAxisAngle(Part.CFrame.lookVector * -1, math.pi/4)
end
else
if Part.Name == "Hood" then
RotationPoint = Part.Position:FromNormalId() - Vector3.new(0, 0, Part.Size.Z/2)
Angles = CFrame.fromAxisAngle(Part.CFrame.rightVector, math.pi/4)
elseif Part.Name == "Trunk" then
RotationPoint = Part.Position + Vector3.new(0, 0, Part.Size.Z/2)
Angles = CFrame.fromAxisAngle(Part.CFrame.rightVector * -1, math.pi/4)
end
end
end
getVectorUnit(Part)
local RotationCF = CFrame.new(RotationPoint)
local offsetCF = RotationCF:toObjectSpace(Part.CFrame)
Part.CFrame = RotationCF * Angles * offsetCF
end
Rotate(game.Workspace.Hood)
- Breakdown If you cannot tell by skimming the code, the script is editing the Trunk/Hood of a vehicle and is made into a function for the code to be added to the actual project. My script gets the specified part and checks if HingeSurface exists to know which surface is connected as if it had a hinge to rotate about. If HingeSurface does not exist, the vehicle is checked to be a Trunk or Hood because you can't open the hood or trunk of a vehicle on the same axis, they're the opposite. I know I have problems within the code that may not make sense because as I said, I'm no expert but any help is very appreciated!
Please comment or contact me if you require more information; this is the entire script.
