so im making a combat game and i scripted some click combat and some knockback. I tweened the knockback so that the player moves back when their hit. But i’d like to know how i can make it so when the player gets hit from one direction they move to the opposite direction, no mater the direction.
My question is how would i create this with tweening? or any other methods
To do this, you can get the player’s looking direction, reverse it, and there’s your knockback.
local POWER = 20
local lookDirection = char.PrimaryPart.CFrame.LookVector
local knockback = -lookDirection*POWER
--//apply knockback as velocity to something
looking back at your post I don’t know if you meant something else, sorry if that’s the case
You can find the place that they should be knocked back too and then tween there. The above method could work. You could also use the lookvector the player hitting them, or the direction of the ray being cast by the hitting players’ mouse.
If you wanted to tween the player using knockback, you’d have to determine the position.
Which should be just:
local POWER = 20
local lookDirection = char.PrimaryPart.CFrame.LookVector
local knockback = -lookDirection*POWER
local origin = char.PrimaryPart.CFrame
local targetPosition = origin + knockback
--//Tween the player's humanoid root part to 'targetPosition'
This works, but the player will always be thrown directly backwords from the direction they’re facing.
I think that using the hitting player’s mouse you could do something like this:
--Local Script on the hitter's end
local mouse = game.Players.LocalPlayer:GetMouse()
local knockBackEvent = game.ReplicatedStorage.KnockBackEvent
local function getKnockback(hitPlayer)
local dir = mouse.UnitRay.Direction
knockBackEvent:FireServer(hitPlayer, dir)
end
--On the server:
local knockBackEvent = game.ReplicatedStorage.KnockBackEvent
local TweenService = game:GetService("TweenService")
local function doKnockback(player, hitPlayer, dir)
newCFrame = CFrame.new(hitPlayer.Character.PrimaryPart.CFrame.Position)
newCFrame *= CFrame.Angles(dir.X, dir.Y, dir.Z)
newCFrame += newCFrame.LookVector * 20 --Arbitrary power amount
--Tween the player to that position.
end
*Note that dir is a Vector3 and hitPlayer is the hit player’s object, this can be found on the client and passed to the client function.
I used your function and modified it a bit and connected it to the events, and it wouldn’t function.
--Local side
local mouse = game.Players.LocalPlayer:GetMouse()
local knockBackEvent = game.ReplicatedStorage.Events.KnockbackM
function getKnockback(hitPlayer)
local dir = mouse.UnitRay.Direction
knockBackEvent:FireServer(hitPlayer, dir)
end
mouse.Button1Down:Connect(function()
local hit = mouse.Target
if hit then
if hit.Parent:FindFirstChildWhichIsA("Humanoid") then
getKnockback(hit.Parent)
end
end
end)
-- Server side
local knockBackEvent = game.ReplicatedStorage.Events.KnockbackM
local TweenService = game:GetService("TweenService")
function doKnockback(player, hitPlayer, dir)
local newCFrame = CFrame.new(hitPlayer.PrimaryPart.CFrame.Position)
newCFrame *= CFrame.Angles(dir.X, dir.Y, dir.Z)
newCFrame += newCFrame.LookVector * 20 --Arbitrary power amount
--Tween the player to that position.
end
knockBackEvent.OnServerEvent:Connect(doKnockback)