Hello developers! For my game, I made a system where you can hold and move different props. For that I used align orientation and vector velocity. But for some reason when I activate script, it doesn’t immediately start working, but rather prop moves to camera out of nowhere without aligning orientation.
Here’s the script:
local model = prop.Model
local playerInfo = LocalServerInfo.GetLocalPlayerInfo()
playerInfo.isHoldingProp=true
local attachment = Instance.new("Attachment",model.PrimaryPart)
attachment.CFrame=CFrame.new(0,0,0)
local alignOrientation = Instance.new("AlignOrientation",attachment)
alignOrientation.Attachment0=attachment
alignOrientation.Mode=Enum.OrientationAlignmentMode.OneAttachment
alignOrientation.Enabled= true
local linearVelocity = Instance.new("LinearVelocity",attachment)
linearVelocity.Attachment0=attachment
linearVelocity.MaxForce=MaxForce
linearVelocity.Enabled=true
local connection
local holdcoro
holdcoro = coroutine.wrap(function()
while playerInfo.isHoldingProp do
print("Started Holding")
local cameracframe = camera.CFrame
local desiredPosition = cameracframe*CFrame.new(0,0,-CurrentHoldDistance)
local rx,ry,rz = cameracframe:ToOrientation()
alignOrientation.CFrame=CFrame.new() * CFrame.Angles(math.rad(rx),math.rad(ry),math.rad(rz))
--alignOrientation.CFrame=cameracframe.LookVector
local lookVector = (desiredPosition.Position - attachment.WorldCFrame.Position).unit
local distance = (desiredPosition.Position - attachment.WorldCFrame.Position).Magnitude
local speed = (distance/0.1)
if speed < 0.05 then speed = 0 end
local velocity = lookVector*(distance/0.1)
print("Current VectorVelocity Velocity: ",velocity)
linearVelocity.VectorVelocity=velocity
--model:SetPrimaryPartCFrame(desiredPosition)
task.wait(0.05)
end
end)
holdcoro()
Here’s a video of it: