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 8 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

1if 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()

1else
2    B=0
3    B1=false
4    deb=false
5end
6end

end

1    end
2end

end)

end

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

2 answers

Log in to vote
0
Answered by 8 years ago
Edited 8 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.

1print(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.

01    local plyr = game.Players.LocalPlayer
02    local BoostForce = 50 --50 studs/sec; changeable
03 
04    while true do
05    db=false
06    j=0
07    repeat wait(.1) until plyr.Character
08    local InputKey = "ButtonB"
09    deb=false
10    B1=false
11    B=0
12 
13    local UserInputService = game:GetService("UserInputService")
14 
15    UserInputService.InputBegan:connect(function(UserInput)
View all 66 lines...


Hope it helps!


Edit:

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

1plyr.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 — 8y
Ad
Log in to vote
0
Answered by 8 years ago
01    local plyr = game.Players.LocalPlayer
02 
03    while true do
04    db=false
05    j=0
06    repeat wait(.1) until plyr.Character
07    local InputKey = "ButtonB"
08    deb=false
09    B1=false
10    B=0
11 
12    local UserInputService = game:GetService("UserInputService")
13 
14    UserInputService.InputBegan:connect(function(UserInput)
15    if  UserInput.UserInputType == Enum.UserInputType.Gamepad1  then
View all 65 lines...
0
this ok? ace12345678135 50 — 8y

Answer this question