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

What's the best way to apply a force to a character?

Asked by
MrHerkes 166
5 years ago
Edited 5 years ago

I wanted a character to get pushed away without any lag. I've tried two ways of applying a force to a character. One is that I applied BodyForce to the root of the character and destroying the BodyForce after 0.1 seconds. Sadly, when someone has high latency, it wouldn't work sometimes. I've also tried the velocity property on the root of the character. It works in studio, but not in the server.

EDIT: What I mean is an explosion-like force to a character.

0
Pretty sure you can just keep the BodyForce on the player for as long as you like? plasmascreen 143 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago

Usually I use BodyPosition to apply force. I do something similar to what you mentioned.

ui.InputBegan:Connect(function(Key, Chatting)
    if Chatting == false then
        if Key.KeyCode == Enum.KeyCode.F then   
            local bf = Instance.new("BodyPosition")
            bf.MaxForce = Vector3.new(0,math.huge,0)
            bf.Position = Character.HumanoidRootPart.Position + Vector3.new(0,15,0)
            bf.P = 7500
            bf.D = 550
            bf.Parent = Character.HumanoidRootPart
            while Jumped do
                wait()
                if math.floor(Character.HumanoidRootPart.Position.Y) <= (math.floor(bf.Position.Y) + 2) and math.floor(Character.HumanoidRootPart.Position.Y) >= (math.floor(bf.Position.Y) -2) then
                    bf:Destroy()
                    break
                end
            end
        end
    end
end)

on this script I create a BodyPosition on the Character.HumanoidRootPart so it moves the entire character. I put the goal 15Y+ from the current position, and then let it run smoothly.

When the position reaches a value that's more than 15 by 2, or less than 15 by 2, it will instantly be destroyed to give momentum to the character and also make it look more smooth.

If you're curious about the functions I used and what they do, I used:

math.floor to turn the value into an absolute value. Example of that would be 20.343434 turned into 20.

Ad

Answer this question