Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Is it possible to use BodyVelocity to drag a player towards a part continuously?

Asked by 2 years ago
Edited 2 years ago

I want to bring a player towards a part using BodyVelocity when E is held, but when the player actually reaches the part, it keeps going forward forever. I have fixed it once by destroying the BodyVelocity once it reaches, but that's not what I'm after. I'm thinking if it is possible for the player to continue moving towards the part even when it reaches so that the player doesn't just fall to the ground if the part is floating. This is my script so far


local player = game:GetService("Players").LocalPlayer local character = player.Character or player.CharacterAdded:Wait() local humanoidRootPart = character:WaitForChild("HumanoidRootPart") local Part = workspace.Part local moveSpeed = 50 local UserInputService = game:GetService("UserInputService") local Direction = (humanoidRootPart.Position - Part.Position).Unit UserInputService.InputBegan:Connect(function(input) if input.KeyCode == Enum.KeyCode.E then EHeld = true local bodyVelocity = Instance.new("BodyVelocity", humanoidRootPart) bodyVelocity.MaxForce = Vector3.new(999999, 999999, 999999) bodyVelocity.P = 50000 bodyVelocity.Velocity = -Direction * moveSpeed UserInputService.InputEnded:Connect(function(input) if input.KeyCode == Enum.KeyCode.E then bodyVelocity:Destroy() end end) end end)

1 answer

Log in to vote
0
Answered by
Puppynniko 1059 Moderation Voter
2 years ago
Edited 2 years ago

Try LineForce A LineForce is used to apply a force along a line between two points. As the end points of the line move, the direction of the force will change accordingly. this simple script will move a part near Part2 Position until its near it then it will stop

local Part = script.Parent.Part2
local Part2 = script.Parent.Part1
local LineF = Part.LineForce

while task.wait() do
    local Dist = (Part.Position-Part2.Position).Magnitude
    print(Dist)
    if Dist < 10 then
        Part.AssemblyLinearVelocity = Vector3.new(0,0,0) --stops it
        LineF.Magnitude = 0
    else
        LineF.Magnitude = 500
    end
end

Full script Hold E and jump to fly torwards the part

local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
local Part = workspace.Part -- the only thing you need to change
local LineF = nil
local moveSpeed = 10
local Holding = false
local UserInputService = game:GetService("UserInputService")
local Con
UserInputService.InputBegan:Connect(function(input)
    if input.KeyCode == Enum.KeyCode.E then 
        Holding = true
        print(":(")
        local Direction = (humanoidRootPart.Position - Part.Position).Unit
        local LineF = Instance.new("LineForce")
        local Attach1 = Instance.new("Attachment")
        local Attach2 = Instance.new("Attachment")
        Attach1.Parent = humanoidRootPart
        Attach2.Parent = Part
        LineF.Attachment0 = Attach1
        LineF.Attachment1 = Attach2
        LineF.Magnitude = 500*moveSpeed
        LineF.Parent = humanoidRootPart
        Con = UserInputService.InputEnded:Connect(function(input)
            if input.KeyCode == Enum.KeyCode.E then
                Holding = false
                Attach1:Destroy()
                Attach2:Destroy()
                LineF:Destroy()
                Con:Disconnect()
            end
        end)
    end
end)
while Holding do
    task.wait()
    if LineF then
        local Dist = (Part.Position-humanoidRootPart.Position).Magnitude
        print(Dist)
        if Dist < 10 then
            Part.AssemblyLinearVelocity = Vector3.new(0,0,0) --stops it
            LineF.Magnitude = 0
        end
    end
end
0
Everything works well! But is it possible to make it so that I wouldn't need to jump? RainDroutz 23 — 2y
0
increase the force Puppynniko 1059 — 2y
Ad

Answer this question