This should work:
local JumpDistance = 80 -- In studs
local JumpHeight = 10 -- Also in studs
local JumpTime = 1 -- In seconds. How long the jump will take
local KeyInterpolationMode = Enum.KeyInterpolationMode.Linear -- Feel free to experiment with changing this
JumpTime = 1 / JumpTime -- This is so we can multiply the delta instead of divide, which will improve performance
local function jumpAttack(character: Model)
local pivot = character:GetPivot()
local half = pivot:PointToWorldSpace(Vector3.new(0, JumpHeight, -(JumpDistance / 2)))
local done = pivot:PointToWorldSpace(-(Vector3.zAxis * JumpDistance))
local curve = Instance.new("Vector3Curve")
curve:X():SetKeys{
FloatCurveKey.new(0, pivot.X, KeyInterpolationMode),
FloatCurveKey.new(0.5, half.X, KeyInterpolationMode),
FloatCurveKey.new(1, done.X, KeyInterpolationMode)}
curve:Y():SetKeys{
FloatCurveKey.new(0, pivot.Y, KeyInterpolationMode),
FloatCurveKey.new(0.5, half.Y, KeyInterpolationMode),
FloatCurveKey.new(1, done.Y, KeyInterpolationMode)}
curve:Z():SetKeys{
FloatCurveKey.new(0, pivot.Z, KeyInterpolationMode),
FloatCurveKey.new(0.5, half.Z, KeyInterpolationMode),
FloatCurveKey.new(1, done.Z, KeyInterpolationMode)}
local x = 0
repeat
x = math.min(x + (JumpTime * task.wait()), 1) -- Increment 'x' using the delta time returned by "task.wait"
local position = curve:GetValueAtTime(x) -- "GetValueAtTime" returns an array...
position = Vector3.new(position[1], position[2], position[3]) -- ...so we'll need to convert it to a Vector3
character:MoveTo(position)
until x == 1
end
I recommend calling the function using task.spawn
, like so:
task.spawn(jumpAttack, Character)
This will prevent any issues from popping up in the future with the repeat until loop blocking any code written below where you’ve called the function from running
It will also make cancelling the jump much easier, since you can use task.cancel
on the thread returned by task.spawn
@GGreenBBoy123 I’ve managed to make a simpler version that uses AlignPosition:
local JumpDistance = 80 -- In studs
local JumpHeight = 10 -- Also in studs
local AlignPosition = Instance.new("AlignPosition")
AlignPosition.Mode = Enum.PositionAlignmentMode.OneAttachment
AlignPosition.ApplyAtCenterOfMass = true
AlignPosition.MaxForce = math.huge
AlignPosition.MaxVelocity = 40 -- Change this value to edit how long the jump takes
local function jumpAttack(Character: Model)
local PrimaryPart = Character.PrimaryPart or Character:WaitForChild("HumanoidRootPart"):: BasePart
local Half = PrimaryPart.CFrame:PointToWorldSpace(Vector3.new(0, JumpHeight, -(JumpDistance / 2)))
local Done = PrimaryPart.CFrame:PointToWorldSpace(-(Vector3.zAxis * JumpDistance))
local AlignPosition = Instance.fromExisting(AlignPosition)
AlignPosition.Position = Half
AlignPosition.Attachment0 = PrimaryPart:WaitForChild("RootAttachment")
AlignPosition.Parent = Character
repeat task.wait() until (PrimaryPart.Position - Half).Magnitude < 0.5
AlignPosition.Position = Done
repeat task.wait() until (PrimaryPart.Position - Done).Magnitude < 0.5
AlignPosition:Destroy()
end
It seems to be more stable than the previous version, and I’d also recommend calling this function using task.spawn
due to the repeat until loops
You can also stop the jump by cancelling the thread, but you’ll also need to destroy the AlignPosition in this version, otherwise the player’s character will be stuck floating in the air
Do make sure to destroy the AlignPosition that’s parented to the player’s character though, not the AlignPosition being used as a template