Let's say you are making a 3D Platformer game and wants to add some abilities. In this tutorial I will be teaching you how to make a Walljump ability.
The walljump will only work for Keyboard. If you want to add mobile support, just change the InputBegan function.
For this system, you will need to insert a LocalScript inside StarterCharacterScripts.
local UserInputService = game:GetService("UserInputService") -- Gets the UserInputService service
local Players = game:GetService("Players") -- Gets the Players service
local Plr = Players.LocalPlayer -- Gets the LocalPlayer
local CanWallJump = true -- Determines if the player can walljump
function GetSurface() -- Returns the surface that the local player is currently standing on
return Plr.Character.Humanoid.FloorMaterial
end
function IsWallHugging() -- Returns the wall that the local player is facing
-- Raycasting takes 3 parameters (Origin: Vector3, Direction: Vector3, RaycastParams: RaycastParams)
-- But we will only be using the first two of them.
local Origin = Plr.Character.PrimaryPart.Position -- The current HumanoidRootPart position
local Direction = Plr.Character.PrimaryPart.CFrame.LookVector -- The direction that the player is currently looking at
local Raycast = workspace:Raycast(Origin, Direction) -- Raycasting
return Raycast and Raycast.Instance or nil -- If Raycast isn't nil then the function will return Raycast.Instance otherwise it will return nil
end
function WallJump() -- Makes the character walljump
local InAir = GetSurface() == Enum.Material.Air -- true or false
local Wall = IsWallHugging() -- Instance or nil
if InAir and Wall and CanWallJump then -- Checks if player is in air and is hugging a wall and can walljump
CanWallJump = false -- Sets CanWallJump to false so the player can't spam jump
Plr.Character.Humanoid:ChangeState(Enum.HumanoidStateType.Jumping) -- Forces the player to jump
-- If you want to add animations, you can play the animation right after forcing the player to jump
task.delay(0.2, function()
CanWallJump = true -- Sets CanWallJump to true 0.2 seconds after forcing the player to jump
end)
end
end
UserInputService.InputBegan:Connect(function(Input, GameProcessedEvent)
if GameProcessedEvent then -- Checks if the game already used the current input
return
end
if Input.KeyCode == Enum.KeyCode.Space then -- Checks if the current input is the space bar
WallJump() -- Tries to walljump
end
end)