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

How do i make a character slide in the direction he or she is facing?

Asked by 7 years ago

this is the code I previously came up with, and, besides the fact that it just gives you a boost every time you press b on the gamepad, it wouldn't work anyways because the boost it was giving me was always in the wrong direction, unless I faced the direction it was giving me a boost in. its hard to describe but here is the code I tried.

local plyr = game.Players.LocalPlayer

while true do db=false j=0 repeat wait(.1) until plyr.Character local InputKey = "ButtonB" deb=false B1=false B=0

local UserInputService = game:GetService("UserInputService")

UserInputService.InputBegan:connect(function(UserInput) if UserInput.UserInputType == Enum.UserInputType.Gamepad1 then

        if UserInput.KeyCode == Enum.KeyCode[InputKey] and deb==false and B1 == false then

B=1 for i = 2,1,-1 do print (B) deb=true B1 = true if UserInput.UserInputType == Enum.UserInputType.Gamepad1 then if UserInput.KeyCode == Enum.KeyCode[InputKey] and deb==true and B1 == true and i>0 and B==1 then plyr.Character.Torso.Velocity=Vector3.new(50,0,0) script.S2:Play() local character = plyr.Character local animation = character.Humanoid:LoadAnimation(script.Slide) animation:Play() wait(.2) B=2 print(B) deb=false wait(.5) animation:Stop()

else
    B=0
    B1=false
    deb=false
end
end

end

    end
end

end)

end

0
Please fix the code blocks. GoldenPhysics 474 — 7y

2 answers

Log in to vote
0
Answered by 7 years ago
Edited 7 years ago

What's the issue?

You're only applying the force to the X axis. You need to equally exert force on all 3 axi.


lookVector

lookVector, a property of CFrame, can return the forward direction that a part is facing. It returns a 1 if it's fully facing an axis or -1 if they're facing the negative side of the axis and it can also return all the numbers in between. It can do this to any axis.

print(workspace.Part.CFrame.lookVector)

1,0,0.84

This part is facing towards the positive X and a little bit towards the positive Z axis.


Final Product

We can do arithmetic with this. I made a new variable called BoostForce which is the max velocity something can move in all directions. In this case I made it 50, you can edit it. I multiplied BoostForce with Vector3.new(1,1,1) to get Vector3.new(50,50,50). Then I multiplied that with the lookvector to get a percentage(1 is 100%, 0.84 is 84% and etc...). If I were to use the part from the last chapter I can multiply the last part's lookVector with Vector3.new(50,50,50) to get: Vector3.new(50,0,42) which is what the part's velocity will be.

    local plyr = game.Players.LocalPlayer
    local BoostForce = 50 --50 studs/sec; changeable

    while true do
    db=false
    j=0
    repeat wait(.1) until plyr.Character
    local InputKey = "ButtonB"
    deb=false
    B1=false
    B=0

    local UserInputService = game:GetService("UserInputService")

    UserInputService.InputBegan:connect(function(UserInput)
    if  UserInput.UserInputType == Enum.UserInputType.Gamepad1  then

            if UserInput.KeyCode == Enum.KeyCode[InputKey] and deb==false and B1 == false then
    B=1
    for i = 2,1,-1
    do

    print (B)

    deb=true

    B1 = true

    if  UserInput.UserInputType == Enum.UserInputType.Gamepad1  then if UserInput.KeyCode == Enum.KeyCode[InputKey] and deb==true and B1 == true and i>0 and B==1 then


    plyr.Character.Torso.Velocity=Vector3.new(1,1,1)*BoostForce*plyr.Character.Torso.CFrame.lookVector
--Instead of Vector3.new(1,1,1)*BoostForce you can also do Vector3.new(BoostForce,BoostForce,BoostForce)
    script.S2:Play()

    local character = plyr.Character

    local animation = character.Humanoid:LoadAnimation(script.Slide)

    animation:Play()

    wait(.2)

    B=2

    print(B)

    deb=false

    wait(.5)

    animation:Stop()

    else
        B=0
        B1=false
        deb=false
    end
    end
end

        end
    end
end)

end


Hope it helps!


Edit:

You can just multiply the lookVector and the BoostForce together only to make it shorter.

plyr.Character.Torso.Velocity=BoostForce*plyr.Character.Torso.CFrame.lookVector

You may use that line instead of the longer one.

0
i tested it out and it still doesnt work like i had hoped, i dont get an error message but the debounce i tried to make doesnt work and the sliding animation i made which i thought would be on the ground just suspends the player in the air for a second while the animation plays out then returns him to his feet. it doesnt even give the character a boost. :/ i know this is on my end and not your fau ace12345678135 50 — 7y
Ad
Log in to vote
0
Answered by 7 years ago
    local plyr = game.Players.LocalPlayer

    while true do
    db=false
    j=0
    repeat wait(.1) until plyr.Character
    local InputKey = "ButtonB"
    deb=false
    B1=false
    B=0

    local UserInputService = game:GetService("UserInputService")

    UserInputService.InputBegan:connect(function(UserInput)
    if  UserInput.UserInputType == Enum.UserInputType.Gamepad1  then

            if UserInput.KeyCode == Enum.KeyCode[InputKey] and deb==false and B1 == false then
    B=1
    for i = 2,1,-1
    do

    print (B)

    deb=true

    B1 = true

    if  UserInput.UserInputType == Enum.UserInputType.Gamepad1  then if UserInput.KeyCode == Enum.KeyCode[InputKey] and deb==true and B1 == true and i>0 and B==1 then


    plyr.Character.Torso.Velocity=Vector3.new(50,0,0)

    script.S2:Play()

    local character = plyr.Character

    local animation = character.Humanoid:LoadAnimation(script.Slide)

    animation:Play()

    wait(.2)

    B=2

    print(B)

    deb=false

    wait(.5)

    animation:Stop()

    else
        B=0
        B1=false
        deb=false
    end
    end
end

        end
    end
end)

end
0
this ok? ace12345678135 50 — 7y

Answer this question