What do you want to achieve?
I want to add a knockback system that would knock the player in the direction the ball is traveling
What is the issue?
I have attempted to add this system, but it doesn’t usually work as intended. Sometimes the player would be knocked into the air, in the opposite direction of where the ball is traveling or doesn’t move anywhere.
What solutions have you tried so far?
I use LinearVelocity to mimic knockback inside the player which sometimes works and sometimes doesn’t. I notice that the problem is that I am using the ball’s position which sometimes does produce the position immediately resulting in weird knock directions. I guess a secondary question to this is how could I get the information of the direction the ball is traveling?
Here is my current code that replicates this knockback. Imagine a scenario where you are throwing a moving dodgeball in which when it hits the player, the code below is played.
local function applyForce(ballCFrame, ballImpactForce, opponentCharacter)
local humanoid = opponentCharacter.Humanoid
------------------------------------------------------------
local PushForce = Instance.new("LinearVelocity")
PushForce.Name = "PushForce"
PushForce.MaxForce = 1000000
ballCFrame = ballCFrame * CFrame.new(0, 0, 100)
PushForce.VectorVelocity = (ballCFrame.lookVector) * ballImpactForce
PushForce.Parent = opponentCharacter.HumanoidRootPart
local attachment = Instance.new("Attachment")
attachment.Parent = opponentCharacter.HumanoidRootPart
PushForce.Attachment0 = attachment
task.spawn(function()
task.wait(0.25)
PushForce:Destroy()
end)
end
Here are some visual pictures to demonstrate what I want to happen.
First get the direction of the ball which would be something like so:
local direction = ballImpactForce.Unit
-- ballCFrame.lookVector would only work if the ball is AIMED at the direction
-- which it is going, which I honestly doubt.
-- I am assuming ballImpactForce is equal to the AssemblyLinearVelocity
Now we will multiply it to make the force stronger
local strength = 10
local vectorVelocity = direction * strength
I actually don’t know what AssemblyLinearVelocity is, but ballImpactForce represents how strong the knockback is, which is a single constant number that gets passed when the ball touches the player, which in this case represents the strength. I will attempt to try what you have written, but let me know if what I said still applies to your solution. Thanks!
Is the Velocity of the ball. If you are using TweenService or anything like that to move the ball or CFrame you’ll have to make the ball look to the direction it is going.
Else instead you can get the direct from ball to player
local direction = (humanoidRootPart.Position - ball.Position).Unit
Now if you are a bit on the left of the ball you’ll be knocked back left instead of back.
Oh I see now. In that case, I will make another parameter for the velocity of the ball which I can translate to the direction you mentioned with ballImpactForce.Unit.
Hey there, sorry it took a while. I have added the direction into the VectorVelocity, but I realize there’s a second problem that still remains in the original post. As I mentioned before the ball sometimes knocks the player in a direction that is not intended to do so such as if the ball smacks from the left side of the character, it would go towards the force of the ball, which I know now why it happens in this video:
I’ve redone the code so we could see where the ball’s position would be when the touched event fires before adding knockback (which I have also tested without the delay of the ball)
More information about this, the ball touch event fires inside a local script (which I know there is a security risk to this), which later fires a knockback RemoteEvent to the server. I’m seeing that using AssemblyLinearVelocity is working a little better, but I will need to tweak it a bit so the touch event would get the direction before it bounces off the player model. Could the problem be due to the Roblox server being slow?