The issue you’re facing with upward knockback using the BodyVelocity
instance could be due to a few different factors. Let’s walk through the potential issues and how you can fix them.
1. Deprecation of BodyVelocity
:
You are correct that BodyVelocity
is deprecated in Roblox, and while it’s not broken, it’s no longer recommended to use it for new projects. It’s being replaced by BodyMover
objects like LinearVelocity
. However, if you’re still using BodyVelocity
, it should work as long as you set it up properly. If you don’t have any immediate restrictions to avoid it, we can still troubleshoot your code.
2. Setting the Velocity Properly:
Looking at your code, you are setting BV.Velocity
based on the direction
input. In particular, for the “Up” direction, you’re multiplying the HumanoidRootPart.CFrame.UpVector
by the knockback force (kb
). This should technically work to create an upward knockback, but there are a couple of things to consider:
- The
MaxForce
you’re setting for BodyVelocity
could limit the effect of the force you’re applying. You’re setting MaxForce = Vector3.new(5e4, 5e2, 5e4)
, which means the force applied in the Y (vertical) direction is much weaker than in the X and Z directions. To make sure upward knockback works properly, you should make the Y-force comparable to or stronger than the X and Z components.
- The
Velocity
you’re setting needs to be realistic for the character’s mass and gravity. Roblox applies a global force (gravity) that affects everything, so if the upward knockback is too weak, the character will quickly fall due to gravity.
3. Suggestions for Improvement:
You can tweak the MaxForce
values and ensure that you’re applying a proper amount of force in the Y direction to counteract gravity effectively. Here’s an improved version of your code with adjustments to MaxForce
and Velocity
:
Copiar código
function module.BodyVelocity(charAttacking, char, kb, direction)
-- Create a BodyVelocity instance
local BV = Instance.new("BodyVelocity")
-- Set MaxForce with a reasonable force in all directions
BV.MaxForce = Vector3.new(50000, 50000, 50000) -- Increase Y force to match X and Z
-- Set the velocity based on the direction
if direction == "Front" then
BV.Velocity = charAttacking.HumanoidRootPart.CFrame.LookVector * kb
elseif direction == "Left" then
BV.Velocity = (charAttacking.HumanoidRootPart.CFrame.RightVector * -1) * kb
elseif direction == "Up" then
BV.Velocity = charAttacking.HumanoidRootPart.CFrame.UpVector * kb
end
-- Parent the BodyVelocity to the character's HumanoidRootPart
BV.Parent = char.HumanoidRootPart
-- Clean up BodyVelocity after a short period to avoid any performance issues
game.Debris:AddItem(BV, 0.16)
end
4. Ensure the kb
Value is Sufficient:
The variable kb
represents the knockback force. If kb
is too small, the knockback effect may not be noticeable. Make sure that kb
is large enough to have a visible effect, especially when working with vertical knockback.
5. Alternative Method (Using LinearVelocity
):
As mentioned earlier, BodyVelocity
is deprecated, and it’s recommended to use LinearVelocity
for new projects. Here’s how you could implement a similar upward knockback effect using LinearVelocity
instead:
Copiar código
function module.LinearVelocityKnockback(charAttacking, char, kb, direction)
-- Create a LinearVelocity instance
local LV = Instance.new("LinearVelocity")
-- Set MaxForce (make sure it's high enough to apply a noticeable force)
LV.MaxForce = Vector3.new(50000, 50000, 50000)
-- Set the velocity based on the direction
if direction == "Front" then
LV.Velocity = charAttacking.HumanoidRootPart.CFrame.LookVector * kb
elseif direction == "Left" then
LV.Velocity = (charAttacking.HumanoidRootPart.CFrame.RightVector * -1) * kb
elseif direction == "Up" then
LV.Velocity = charAttacking.HumanoidRootPart.CFrame.UpVector * kb
end
-- Parent the LinearVelocity to the character's HumanoidRootPart
LV.Parent = char.HumanoidRootPart
-- Clean up LinearVelocity after a short period to avoid performance issues
game.Debris:AddItem(LV, 0.16)
end
In this case, LinearVelocity
works similarly to BodyVelocity
but is designed for more modern physics in Roblox. It’s a better choice for new projects.
Conclusion:
- Ensure that the
MaxForce
is high enough for the Y-direction to create noticeable upward knockback.
- Use a larger value for
kb
to apply a more substantial force.
- Consider switching to
LinearVelocity
for better performance and future-proofing.
I hope this helps to resolve the issue with upward knockback! Let me know if you have more questions.