Press X to Jump Then While You re in the Air Press X Again to Double Jump
Double jumping is a feature that appears in many games. This tutorial will cover how to implement it in Roblox.
Setting Up
To go started, you volition demand to insert a LocalScript
into StarterPlayerScripts
. You can proper noun it any you desire, but you should name it something descriptive so you tin find it quickly if you demand to. In this tutorial, it will be named DoubleJump
.
In this script, y'all will demand to become the player's character and that character'southward humanoid. The humanoid is the controller inside the character model that allows it to move and jump.
local localPlayer = game.Players.LocalPlayer local character local humanoid local function characterAdded(newCharacter) character = newCharacter humanoid = newCharacter:WaitForChild("Humanoid") terminate if localPlayer.Grapheme and so characterAdded(localPlayer.Character) end localPlayer.CharacterAdded:connect(characterAdded)
The characterAdded
function takes a grapheme model as an argument and stores that grapheme into the variable character
. It also stores that character'due south humanoid in the humanoid
variable. You should use Instance/WaitForChild
instead of simply accessing newCharacter.Humanoid
, equally when the character outset loads information technology could take a chip of fourth dimension for the humanoid to load.
The characterAdded
role is bound to the player's Player/CharacterAdded
result. Binding to CharacterAdded makes sure that this function is called every fourth dimension the player respawns. Nonetheless, sometimes on game initialization this script will run after the first thespian is spawned. To cover this instance, you also need to phone call characterAdded
on the player'south graphic symbol if it already exists.
Double Jumping
To make the player's character double jump, it is important to know how the default jump works. The humanoid example which controls movement and jumping of characters has a congenital-in land machine that says how it should behave. When a histrion jumps, the country of their humanoid is set to Jumping, which applies an upwardly strength to the character model. A brief moment later, the humanoid is set to the Freefall state, which lets physics take over and pull the character back to the basis with gravity. When the character hits the ground again the state of the humanoid is set briefly to landed.
A uncomplicated way to implement double bound is to force the character back into the Jumping state. We tin also listen to when the state changes dorsum to Landed to forbid the character from double jumping more than once.
You can employ Humanoid/ChangeState
to force the character back into the jumping state in order to cause the double bound.
To make the thespian's character double jump you will too demand to know when the player pressed the jump input. To do this y'all tin use an event of UserInputService called UserInputService/JumpRequest
. This event is fired whenever the player tries to jump.
Add the highlighted code to your script:
local UserInputService = game:GetService("UserInputService") local localPlayer = game.Players.LocalPlayer local character local humanoid local canDoubleJump = simulated local hasDoubleJumped = false function onJumpRequest() if not grapheme or not humanoid or non character:IsDescendantOf(workspace) or humanoid:GetState() == Enum.HumanoidStateType.Dead and then render end if canDoubleJump and non hasDoubleJumped and so hasDoubleJumped = true humanoid:ChangeState(Enum.HumanoidStateType.Jumping) end end local role characterAdded(newCharacter) character = newCharacter humanoid = newCharacter:WaitForChild("Humanoid") humanoid.StateChanged:connect(function(old, new) if new == Enum.HumanoidStateType.Freefall then canDoubleJump = truthful terminate cease) end if localPlayer.Character then characterAdded(localPlayer.Character) stop localPlayer.CharacterAdded:connect(characterAdded) UserInputService.JumpRequest:connect(onJumpRequest)
JumpRequest is spring to the function onJumpRequest
. This function offset checks if these conditions are met:
- The
character
andhumanoid
variables are not zilch - The character is currently somewhere in the workspace
- The character is currently spawned
If any of these are not fulfilled, the code stops executing by calling render
. If all of them are, it moves on to the next statement. This is to forestall double jumping when there is no graphic symbol.
A player can double jump if the Humanoid is currently in freefall and has not already double jumped. The first of these conditions tin be checked with canDoubleJump
which is prepare to true when the humanoid is in the Freefall state. The next condition, making certain the player hasn't already done a double jump, can exist handled with a technique called Debounce. In this example, the variable hasDoubledJumped
is initially false but gets prepare to true equally soon as the humanoid performs the double jump.
If the conditions are right for a double leap, you can phone call ChangeState to forcefulness the spring.
Resetting
Although the player can now double leap, there is all the same some cleanup to do. In your testing, you probably noticed that you can but double jump once; subsequently that, you cannot do it again. This is a simple fix: when the player has landed, reset canDoubleJump
and hasDoubleJumped
to imitation.
local UserInputService = game:GetService("UserInputService") local localPlayer = game.Players.LocalPlayer local graphic symbol local humanoid local canDoubleJump = imitation local hasDoubleJumped = false function onJumpRequest() if non graphic symbol or non humanoid or non character:IsDescendantOf(workspace) or humanoid:GetState() == Enum.HumanoidStateType.Expressionless and so render end if canDoubleJump and not hasDoubleJumped then hasDoubleJumped = truthful humanoid:ChangeState(Enum.HumanoidStateType.Jumping) end cease local function characterAdded(newCharacter) graphic symbol = newCharacter humanoid = newCharacter:WaitForChild("Humanoid") hasDoubleJumped = false canDoubleJump = false humanoid.StateChanged:connect(function(onetime, new) if new == Enum.HumanoidStateType.Freefall then canDoubleJump = truthful elseif new == Enum.HumanoidStateType.Landed then canDoubleJump = false hasDoubleJumped = false end end) terminate if localPlayer.Character and then characterAdded(localPlayer.Character) end localPlayer.CharacterAdded:connect(characterAdded) UserInputService.JumpRequest:connect(onJumpRequest)
Tuning
If you test the code so far you may take noticed that if yous hold the spacebar, the double jump looks more like a bigger spring than a second one. This is considering Enum/HumanoidStateType/Freefall
is fix before the player starts to really fall. To fix this, and make the double jump wait more like a 2d bound, y'all should wait before setting canDoubleJump
to truthful.
Yous tin can also brand the second leap more pronounced past increasing the jump ability for the 2d bound. You can prepare the force of the leap with Humanoid/JumpPower
. Just exist sure to reset JumpPower back to its initial value once the character has landed.
local UserInputService = game:GetService("UserInputService") local localPlayer = game.Players.LocalPlayer local character local humanoid local canDoubleJump = faux local hasDoubleJumped = simulated local oldPower local TIME_BETWEEN_JUMPS = 0.two local DOUBLE_JUMP_POWER_MULTIPLIER = ii function onJumpRequest() if not character or non humanoid or non character:IsDescendantOf(workspace) or humanoid:GetState() == Enum.HumanoidStateType.Dead then render end if canDoubleJump and not hasDoubleJumped so hasDoubleJumped = true humanoid.JumpPower = oldPower * DOUBLE_JUMP_POWER_MULTIPLIER humanoid:ChangeState(Enum.HumanoidStateType.Jumping) end end local part characterAdded(newCharacter) graphic symbol = newCharacter humanoid = newCharacter:WaitForChild("Humanoid") hasDoubleJumped = simulated canDoubleJump = false oldPower = humanoid.JumpPower humanoid.StateChanged:connect(function(sometime, new) if new == Enum.HumanoidStateType.Landed then canDoubleJump = false hasDoubleJumped = false humanoid.JumpPower = oldPower elseif new == Enum.HumanoidStateType.Freefall then wait(TIME_BETWEEN_JUMPS) canDoubleJump = true cease end) terminate if localPlayer.Character then characterAdded(localPlayer.Character) end localPlayer.CharacterAdded:connect(characterAdded) UserInputService.JumpRequest:connect(onJumpRequest)
Anyone who joins your game can at present double jump every bit they wish.
Conclusion
Double jumping is a useful mechanic to add depth to your game. In that location are some things that you might want to tweak, such equally TIME_BETWEEN_JUMPS
and DOUBLE_JUMP_POWER_MULTIPLIER
, to alter how long later the first jump a player must await before double jumping or the ability of the double jump respectively.
Source: https://developer.roblox.com/en-us/articles/Double-Jumping
0 Response to "Press X to Jump Then While You re in the Air Press X Again to Double Jump"
Postar um comentário