I’ve been trying to implement a knockback feature into my game. It mostly works, but there is a glaring issue. (I believe) the knockback direction depends on which direction the target’s back is facing. I edited the orientation of the target to make it so that it would equal that of the attacker but spinned 180 degrees, but this does not apply when the target is in shiftlock. This is the server-side code:
I have a very similar script in my game.
The main point is
rotatedCFrame = CFrame.Angles(0, math.rad(-180), 0)–this is backwards
local simCFrame= char.HumanoidRootPart.CFrame:ToWorldSpace(rotatedCFrame)
local newvector=simCFrame.LookVector
slide.Velocity = newvector * reps
So we are getting the CFrme of the character transformed by -180 degrees on the y axis and using that to calculate the direction of the velocity. Which is backwardsi n this case.
function KnockBack(char,reps)
if char:FindFirstChild("Knockback") ==nil and reps>1 then
if game.Players:GetPlayerFromCharacter(char)==nil then
local kb=Instance.new("BoolValue")
kb.Name="Knockback"
kb.Parent=char
local char=char
char.Humanoid:ChangeState(Enum.HumanoidStateType.Running)
local slide = Instance.new("BodyVelocity")
local rotatedCFrame
rotatedCFrame = CFrame.Angles(0, math.rad(-180), 0)
local simCFrame= char.HumanoidRootPart.CFrame:ToWorldSpace(rotatedCFrame)
slide.MaxForce = Vector3.new(1,0,1) * 1000000 --you can change this to whatever number you see fit. I prefer this one
local newvector=simCFrame.LookVector
slide.Velocity = newvector * reps
slide.Parent = char.HumanoidRootPart
--giveForcefield(char,1) -- gives you invincibility for 1 second (optimal for the duration of the dodge roll)
slide.Velocity*= 3
for count = 1, reps do
task.wait(0.02)
slide.Velocity*= 0.75
end
Debris:AddItem(kb,1)
slide:Destroy()
end
end
end
So this is a knockback function. When activated it knocks back the character based on reps which is repetition. The way I programmed it was to get the damage and divide it by like 5 or 10 to get the reps.
Also the problem with using the shift-lock could be due to Humanoid.AutoRotate=true
trying turning it to autorotate=false while you are doing your knockback. This will allow you to transform the orientation of the target
This is using body velocity. Which is getting a direction cfram=char.HumanoidRootPart.CFrame:ToWorldSpace(rotatedCFrame)
This new CFrame has a offset we can apply based on its orientation. cfram=cfram:ToWorldSpace(0,0,2)–since the CFrame is facing backwards positive Z transformation will give us the vector position of behind it.
so the Vector would be cfram.Position