What is the issue? Why can’t Dash animations only be played in place?
robloxapp-20240215-1818451.wmv (950.7 KB)
The above is a video, which is really difficult for me. In fact, I cannot fully understand this code because I did it according to the forum’s tutorial
local RunService = game:GetService("RunService")
local RS = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local UIS = game:GetService("UserInputService")
local CAS = game:GetService("ContextActionService")
local Promise = require(RS.Modules.Promise)
local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local HRP = Character:WaitForChild("HumanoidRootPart")
local Humanoid = Character:WaitForChild("Humanoid")
local DashAnimsFolder = RS.Assets.Animations.Dash
print(DashAnimsFolder)
local directions = {
['Forward'] = Vector3.new(0,0,-1),
['Left'] = Vector3.new(-1,0,0),
['Backward'] = Vector3.new(0,0,1),
["Right"] = Vector3.new(1,0,0)
}
local anims = {
}
for index,animation in pairs(DashAnimsFolder:GetDescendants()) do
if animation:IsA("Animation") then
local anim = Humanoid:LoadAnimation(animation)
print(anim)
anims[animation.Name] = anim
end
end
function accelerateToVelo(lv:LinearVelocity,targetVelocitoy:Vector2,accel)
local velocity:Vector2 = lv.PlaneVelocity
local deltaV:Vector2 = targetVelocitoy - velocity
local _,current_dt = game:GetService("RunService").Stepped:Wait()-- 等待每一帧更新
local maxAccel = deltaV/current_dt--每一帧最大加速度
local finalAcc = (maxAccel.Magnitude < accel) and maxAccel or maxAccel.Unit*accel
return Promise.new(function(resolve)
local e
local FRAMES_WITHIN_REACH = 1
e = game:GetService("RunService").Stepped:Connect(function(time,dt)
lv.PlaneVelocity = velocity + finalAcc*dt
if(targetVelocitoy - lv.PlaneVelocity).Magnitude <= finalAcc.Magnitude*dt*FRAMES_WITHIN_REACH then
lv.PlaneVelocity = targetVelocitoy
e:Disconnect()
resolve()
end
end)
end)
end
function accelerateToDistance(targetVelocity:Vector2,accTime,totalDistance)
local Attachment = Instance.new("Attachment",HRP)
local LV = Instance.new("LinearVelocity")
LV.MaxForce = math.huge
LV.VelocityConstraintMode = Enum.VelocityConstraintMode.Plane
LV.PlaneVelocity = Vector2.zero
LV.Attachment0 = Attachment
LV.RelativeTo = Enum.ActuatorRelativeTo.World
LV.PrimaryTangentAxis = Vector3.xAxis
LV.SecondaryTangentAxis = Vector3.zAxis
local d1 = 0.5*(targetVelocity.Magnitude)*accTime
local d2 = totalDistance - d1
local a2 = math.abs(-(targetVelocity.Magnitude*targetVelocity.Magnitude)/(2*d2))
return accelerateToVelo(LV,targetVelocity,targetVelocity.Magnitude/accTime):andThen(function()
return accelerateToVelo(LV,Vector2.zero,a2):andThen(function()
Attachment:Destroy()
end)
end)
end
local isDashing = false
function onDashKeyPressed(actionName,inputState,inputObject)
if inputState == Enum.UserInputState.Begin and not isDashing then
isDashing = true
doDash():andThen(function()
isDashing = false
end)
end
end
CAS:BindAction("DASH_ACTION",onDashKeyPressed,true,Enum.KeyCode.Q)
function doDash()
local animation = anims['ForwardDash']
local movingDir = (HRP.AssemblyLinearVelocity*Vector3.new(1,0,1)).Unit
if UIS.MouseBehavior == Enum.MouseBehavior.LockCenter then
local lastClosest = math.huge
local animDirection
for directionName,dirVec in directions do
local closeness = 1 - (HRP.CFrame +dirVec):Dot(HRP.AssemblyLinearVelocity.Unit)
if closeness < lastClosest then
lastClosest = closeness
animDirection = directionName
end
end
animation = anims[animDirection.."Dash"]
end
if HRP.AssemblyLinearVelocity.Magnitude == Vector3.zero then
movingDir = (Vector3.new(1,0,1)*-HRP.CFrame.LookVector).Unit
animation = anims['BackwardDash']
end
animation:Play(.25,2,1.2)
local targetVel = 60
accelerateToDistance(targetVel*Vector2.new(movingDir.X,movingDir.Z).Unit,0.04,18)
return Promise.new(function(resolve)
animation.Stopped:Wait()
resolve()
end)
end