i’m making an ability system, one ability is meant to knock enemies back by using a linear velocity
the direction of the linear velocity is simply the look vector of the enemy’s root part but flipped
the lookvector, however, is left and right, and also a red arrow
and unless i’ve been living in lies all my life, one of the BLUE arrows represent the look vector
the enemy moves to the left without flipping the look vector, and moves to the right when flipping the vector
i tried calculating the direction by simply giving the player origin, and then getting the direction of that and flipping it:
local direction = (playerOrigin - enemyOrigin).Unit * (-1)
but this resulted in the same error
i am really confused, and don’t know what to do
function utility.CreateLinearVelocityInPart(part: BasePart, direction: number, velocity: number)
local attachment = Instance.new('Attachment')
local linearVelocity = Instance.new('LinearVelocity')
attachment.Parent = part
attachment.Name = 'knockbackAttachment'
attachment.Position = part.Position
linearVelocity.Name = 'knockbackVelocity'
linearVelocity.MaxForce = 999999999
linearVelocity.RelativeTo = Enum.ActuatorRelativeTo.Attachment0
linearVelocity.VectorVelocity = (direction * velocity)
linearVelocity.Attachment0 = attachment
linearVelocity.Parent = attachment
return linearVelocity
end
function utility.ApplyKnockback(character: Model, classData: {string: any}, stunDuration: number)
local knockback: number? = classData.abilityFlags.knockback
if not knockback then return end
local knockbackDirection = character.PrimaryPart.CFrame.LookVector
local stunned = utility.GetGameplayValue(character, 'stunned')
local damageResistance = utility.GetGameplayValue(character, 'damageResistance')
local enemyData = enemyStats[character.Name]
local baseVelocity = math.floor(knockback / damageResistance.Value)
local enemyMass = enemyData.mass or 50
local referenceMass = 50
local exponent = 0.5
local finalVelocity
if enemyMass > baseVelocity then
local scale = (referenceMass / enemyMass) ^ exponent
finalVelocity = baseVelocity * scale
else
finalVelocity = baseVelocity
end
local velocityDuration = (stunDuration * 0.35)
local linearVelocity = utility.CreateLinearVelocityInPart(character.PrimaryPart, knockbackDirection, finalVelocity)
velocityDuration = utility.ConvertToDecimalPoints(velocityDuration, 100)
print(`Final knockback velocity is {finalVelocity}`)
print(`Final velocity duration is {velocityDuration}`)
--dService:AddItem(linearVelocity.Parent, velocityDuration)
if enemyData.canBeStunned then
stunned.Value = true
task.delay(stunDuration, function()
stunned.Value = true
end)
end
end