Skip to content

Instantly share code, notes, and snippets.

@howmanysmall
Last active April 6, 2023 16:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save howmanysmall/39ec29093fad83d0dd73810dd5b1f12f to your computer and use it in GitHub Desktop.
Save howmanysmall/39ec29093fad83d0dd73810dd5b1f12f to your computer and use it in GitHub Desktop.
determines if you can backstab
-- Compiled with roblox-ts v2.1.0
local NORMALIZER_VECTOR3 = Vector3.new(1, 0, 1)
local CanBackstab = function(attacker, victim)
local attackerHead = attacker:FindFirstChild("Head")
if attackerHead and attackerHead:IsA("BasePart") then
local victimHead = victim:FindFirstChild("Head")
if victimHead and victimHead:IsA("BasePart") then
local _exp = victim:GetPivot().Position * NORMALIZER_VECTOR3
local _arg0 = attacker:GetPivot().Position * NORMALIZER_VECTOR3
local attackerToVictim = (_exp - _arg0).Unit
local attackerEyeLevel = (attackerHead.Position * NORMALIZER_VECTOR3).Unit
local victimEyeLevel = (victimHead.Position * NORMALIZER_VECTOR3).Unit
return not (attackerToVictim:Dot(victimEyeLevel) <= 0 or (attackerToVictim:Dot(attackerEyeLevel) <= 0.5 or attackerEyeLevel:Dot(victimEyeLevel) <= -0.3))
end
end
return false
end
local default = CanBackstab
return {
CanBackstab = CanBackstab,
default = default,
}
const NORMALIZER_VECTOR3 = new Vector3(1, 0, 1);
export const CanBackstab = (attacker: Model, victim: Model) => {
const attackerHead = attacker.FindFirstChild("Head");
if (attackerHead && attackerHead.IsA("BasePart")) {
const victimHead = victim.FindFirstChild("Head");
if (victimHead && victimHead.IsA("BasePart")) {
const attackerToVictim = victim
.GetPivot()
.Position.mul(NORMALIZER_VECTOR3)
.sub(attacker.GetPivot().Position.mul(NORMALIZER_VECTOR3)).Unit;
const attackerEyeLevel = attackerHead.Position.mul(NORMALIZER_VECTOR3).Unit;
const victimEyeLevel = victimHead.Position.mul(NORMALIZER_VECTOR3).Unit;
return !(
attackerToVictim.Dot(victimEyeLevel) <= 0 ||
attackerToVictim.Dot(attackerEyeLevel) <= 0.5 ||
attackerEyeLevel.Dot(victimEyeLevel) <= -0.3
);
}
}
return false;
};
export default CanBackstab;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment