I am coding a custom “Dragging” system that allows you to pickup objects, rotate them, move them etc…, This is all within First Person.
After a while I was able to calculate the force of a spin, now im just having trouble applying that to the dragged part or “DragTarget” specifically with direction, as it is it seems to only apply the force to the direction in which you look, rather than the direction you span.
Here is the code, If there is any more information you need just ask, thank you.
--[[Dragging]]
local MaxDistance = 10
local HoldDistance = 6.5
local CanCollide = true
local DragConnection
local DragTarget
local DragOrient
--
local CCamera = workspace.CurrentCamera
local LastCFrame = CCamera.CFrame
local CurrentCFrame
local Axis, Angle
local RotationSpeed
--// When Button1Down Start Dragging
Mouse.Button1Down:Connect(function()
if Mouse.Target ~= nil then
--// Loop Locals
DragTarget = Mouse.Target
DragOrient = DragTarget.Orientation
--// Check Target can be dragged
if string.match(Mouse.Target.Name, "%[D%]") and (HRP.Position - DragTarget.Position).Magnitude < MaxDistance then
IsButton1Down = true
--// Play Sound
script.Drag_Start:Play()
--// Update Single Set Info
Mouse.Target.CanCollide = CanCollide
end
--// While Button1Down is true, loop
DragConnection = game:GetService("RunService").RenderStepped:Connect(function()
if IsButton1Down == true then
--// Drag Target
local MouseRay = Mouse.UnitRay
local Offset = MouseRay.Direction * HoldDistance
local TargetPosition = MouseRay.Origin + Offset
DragTarget.Position = TargetPosition
--// Prevent Velocity Movement
DragTarget.AssemblyLinearVelocity = Vector3.new(0,0,0)
DragTarget.AssemblyAngularVelocity = Vector3.new(0,0,0)
DragTarget.Orientation = DragOrient
--// Calculate Drop Velocity
CurrentCFrame = CCamera.CFrame
Axis, Angle = (CurrentCFrame * LastCFrame:Inverse()):ToAxisAngle()
RotationSpeed = Angle / game:GetService("RunService").RenderStepped:Wait()
LastCFrame = CurrentCFrame
--print(RotationSpeed .. " Rad/s")
--// If Distance is past Max then end Loop
if (Player.Character.HumanoidRootPart.Position - DragTarget.Position).Magnitude > MaxDistance then
IsButton1Down = false
--
if DragConnection then
DragConnection:Disconnect()
--// Play Sound
script.Drag_End:Play()
--// Update Single Set Info
Mouse.Target.CanCollide = true
end
end
end
end)
end
end)
--// When Button1Up Stop Dragging
Mouse.Button1Up:Connect(function()
if IsButton1Down == true then
--// Play Sound
script.Drag_End:Play()
--// Update Single Set Info
DragTarget.CanCollide = true
--// Apply Drop Velocity
DragTarget:ApplyImpulse(Mouse.UnitRay.Direction * RotationSpeed * DragTarget.Mass * 2)
DragConnection:Disconnect()
IsButton1Down = false
end
end)