I have this spaceship and its cockpit and I’m trying to get the cockpit to open (which I have done) but it opens strangely:
I changed the pivot position but it still doesn’t seem to work properly. Any idea’s on how to fix?
Edit: here’s my code
local tweenService = game:GetService("TweenService")
local cockpit = workspace["Banshee Class Fighter"].ShipModel.Hinge.BlueCockpit
local proxPrompt = workspace["Banshee Class Fighter"].ShipModel.Hinge.ProximityPrompt
local touch_part = workspace.touchBox
--//PERSONAL NOTES\\--
--2 -- time
--Enum.EasingStyle.Linear -- easing style
--Enum.EasingDirection.Out -- easing direction
--1 -- if less than one, tween will loop indefinitely
--false -- does not reverse once it reaches its goal
--0 -- delay time
local openProperties = {
["Rotation"] = Vector3.new(0,-90,-90)
}
local closedProperties = {
["Rotation"] = Vector3.new(0,-90,0)
}
local tweenInfo = TweenInfo.new(
1.5,
Enum.EasingStyle.Exponential,
Enum.EasingDirection.Out,
0, -- //makes it NOT repeat\\
false,
0.3
)
local openCockpit = tweenService:Create(cockpit, tweenInfo, openProperties)
local closeCockpit = tweenService:Create(cockpit, tweenInfo, closedProperties)
workspace.touchBox.Touched:Connect(function(hit)
openCockpit:Play()
proxPrompt.Enabled = true
end)
workspace.touchBox.TouchEnded:Connect(function(player)
closeCockpit:Play()
proxPrompt.Enabled = false
end)```
The problem seems to be that you’re rotating the whole cockpit over its center and not rotating it over a hinge pivot.
To relieve this problem, you may attempt to rotate the cockpit over the pivot by getting the cockpit’s CFrame, multiplying it by a pivot offset, and multiplying that by a rotational CFrame (the amount of degrees to open the cockpit), and that by the negative pivot offset, resulting something like this (this code is just a template: Adapt it to your particular needs):
local TweenService = game:GetService("TweenService")
local part = -- the cockpit (if it's a Model, then set it to a part that's as big as the Model and Weld-Constrain the Model to this Part).
local rotation = CFrame.Angles(math.rad(0), math.rad(45), math.rad(0)) -- play around with this until it suits your particular case.
local pivotOffset = Vector3.new(-part.Size.X / 2, part.Size.Y / 2, 0) -- play around with this until it suits your particular case.
local closedCFrame = part.CFrame -- supposing it's closed at the simulation start.
local openCFrame = closedCFrame * CFrame.new(pivotOffset) * rotation * CFrame.new(-pivotOffset)
local tweenInfo = TweenInfo.new(2)
local openTween = TweenService:Create(part, tweenInfo, {CFrame = openCFrame})
local closeTween = TweenService:Create(part, tweenInfo, {CFrame = closedCFrame})
The result of that code applied to two adjacent Parts is this:
The method safeLast120 uses above roughly works for tweens between some low angles for stationary models, but if you want it to rotate exactly around a hinge, you can:
Add an attachment to the spaceship at the hinge point
Add an attachment to the cockpit at the hinge point
Unanchor the cockpit
Add a rigid constraint between the attachments
Tween the CFrame of the attachment in the spaceship to move the cockpit
This is a very flexible way to move objects attached to other objects. It’s nice because it works even if the spaceship is moving around or unanchored.