My bad, AlignOrientation CFrame should not be that of the camera now that I think of it.
The proper way is this (no need to adjust the attachment by 180 degree anymore):
AlignOrientation.CFrame = CFrame.lookAt(riggedPart.Position,Camera.CFrame.Position)
I have tested it along with AlignPosition
to make sure it works this time. Here is the full code, you may find it useful:
local Attachment = Instance.new('Attachment')
local AlignOrientation = Instance.new('AlignOrientation')
AlignOrientation.Mode = Enum.OrientationAlignmentMode.OneAttachment
AlignOrientation.RigidityEnabled = true
AlignOrientation.Attachment0 = Attachment
local AlignPosition = Instance.new('AlignPosition')
AlignPosition.Mode = Enum.PositionAlignmentMode.OneAttachment
AlignPosition.RigidityEnabled = true
AlignPosition.Attachment0 = Attachment
local UIS = game:GetService('UserInputService')
local RS = game:GetService('RunService')
local Camera = workspace.CurrentCamera
local RSConnection
local riggedPart
local distance
local RAY_PARAM = RaycastParams.new()
local MAX_DISTANCE = 1000
local function OnHeartBeat()
local mousePos = UIS:GetMouseLocation()
local ray = Camera:ScreenPointToRay(mousePos.X, mousePos.Y - 36)
AlignPosition.Position = ray.Origin + ray.Direction * distance
AlignOrientation.CFrame = CFrame.lookAt(riggedPart.Position,Camera.CFrame.Position)
end
UIS.InputBegan:Connect(function(input, gpe)
if gpe or riggedPart then return end
if input.UserInputType == Enum.UserInputType.MouseButton1 or input.UserInputType == Enum.UserInputType.Touch then
local mousePos = UIS:GetMouseLocation()
local ray = Camera:ScreenPointToRay(mousePos.X, mousePos.Y - 36)
local rayResult = workspace:Raycast(ray.Origin,ray.Direction * MAX_DISTANCE, RAY_PARAM)
if rayResult and rayResult.Instance:IsA('BasePart') then
riggedPart = rayResult.Instance
AlignPosition.Parent = riggedPart
AlignOrientation.Parent = riggedPart
Attachment.Parent = riggedPart
distance = (rayResult.Position - ray.Origin).Magnitude
RSConnection = RS.Heartbeat:Connect(OnHeartBeat)
end
end
end)
UIS.InputEnded:Connect(function(input,gpe)
if gpe or not riggedPart then return end
if input.UserInputType == Enum.UserInputType.MouseButton1 or input.UserInputType == Enum.UserInputType.Touch then
RSConnection:Disconnect()
AlignPosition.Parent = script
AlignOrientation.Parent = script
riggedPart = nil
end
end)
Have a nice scripting session.
EDIT:
Now that I read Your post again, I am no longer certain that you want the part to be facing the camera all the time?
If you want to have the part in certain constant orientation during the drag, simply put target orientation CFrame into the AlignOrientation
only once during the InputStarted
event, and skip updating it on heartbeat.
riggedPart = rayResult.Instance
AlignPosition.Parent = riggedPart
AlignOrientation.Parent = riggedPart
AlignOrientation.CFrame = CFrame.lookAt(riggedPart.Position,Camera.CFrame.Position) * CFrame.Angles(0,math.pi/2,0) -- just an example
Attachment.Parent = riggedPart
distance = (rayResult.Position - ray.Origin).Magnitude
--on heartbeat remove this line
-- AlignOrientation.CFrame = CFrame.lookAt(riggedPart.Position,Camera.CFrame.Position)
or to preserve original orientation:
AlignOrientation.CFrame = riggedPart.CFrame