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

Why does the character fling further in air using Velocity?

Asked by
Romul 21
5 years ago

The below code flings the player backwards. After some research I found that using the velocity as opposed to BodyForce was a more viable way to do this. This is due to when using BodyForce, if the player hit an object while flinging backwards, they would sporadically fling around after hitting it.

The problem that I have now is the velocity not being consistent based on I'm guessing the players height on the map. When standing on the baseplate in-game, this flings the character back several studs and works fine. If the player jumps then uses this code, they fling about four times further than if they were still grounded. Now..if they were to get on a tall object, let's say the roof of a building, and use this without jumping. It flings even FURTHER back!

What could I be doing wrong? Should I explore other ways to fling the player back? Am I calculating the Velocity wrong? Thanks in advance!

UserInputService.InputBegan:Connect(function(InputObject,Processed)
    if not Processed then
        if InputObject.KeyCode == Enum.KeyCode.X then
            if script.Shift.Value == false then
                script.Shift.Value = true
                animTrack:Play()
                Player.Character.HumanoidRootPart.Velocity = Player.Character.HumanoidRootPart.CFrame.lookVector*-500
                wait(.4)
                animTrack:Stop()
                wait(3)
                script.Shift.Value = false
            end
        end
    end
end)

1 answer

Log in to vote
0
Answered by
Romul 21
5 years ago
Edited 5 years ago

I have fixed this myself. I decided to go with adding BodyVelocity in. I have adjusted the MaxForce so that it didn't sporadically fling the character when they hit an object. This here works flawlessly.

UserInputService.InputBegan:Connect(function(InputObject,Processed)
    if not Processed then
        if InputObject.KeyCode == Enum.KeyCode.W and script.Shift.Value == false then
            script.Shift.Value = true
            animTrack:Play()
            local position = Instance.new("BodyVelocity",Player.Character.HumanoidRootPart)
            position.MaxForce = Vector3.new(50000,50000,50000)
            position.Velocity = Player.Character.HumanoidRootPart.CFrame.lookVector*90
            game.Debris:AddItem(position,.5)
            wait(.4)
            animTrack:Stop()
            wait(3)
            script.Shift.Value = false
        end
    end
end)
Ad

Answer this question