Hello. I made a grab system with the ability to throw an object, but light objects are thrown too hard, or, on the contrary, weakly. Video below.
I want to make the throw speed adjustable depending on the weight of the object. The code is below.
local grabEvent = script.Parent.GrabEvent
local up = false
local curObject
local hrp = script.Parent.Parent:WaitForChild("HumanoidRootPart")
function changePos(alignPos:AlignPosition)
repeat alignPos.Position = script.Parent.GrabPart.Position
wait()
until up
end
grabEvent.OnServerEvent:Connect(function(plr, act, object)
if act == "GrabStart" then
up = false
curObject = object
local alignPosition = Instance.new("AlignPosition", object)
alignPosition.Attachment1 = script.Parent.GrabPart.Attachment
local density = object.CurrentPhysicalProperties.Density/2.5
alignPosition.Responsiveness = 100*2/density
alignPosition.ApplyAtCenterOfMass = true
local att2 = Instance.new("Attachment", curObject)
att2.Name = "GrabAtt"
alignPosition.Attachment0 = att2
elseif act == "GrabEnd" then
up = true
curObject:FindFirstChild("AlignPosition"):Destroy()
curObject:FindFirstChild("GrabAtt"):Destroy()
elseif act == "Camera" then
script.Parent.GrabPart.CFrame = object
elseif act == "Throw" then
up = true
local alignPos = curObject:FindFirstChild("AlignPosition")
alignPos.Attachment0:Destroy()
alignPos:Destroy()
local density = curObject.CurrentPhysicalProperties.Density/2.5
curObject.Velocity = script.Parent.GrabPart.CFrame.LookVector * 50/density
curObject = nil
end
end)
The code hasn’t been polished yet, so don’t be surprised.