Greetings, I’m currently making lightsaber/sword combat system, I want to make it like when player blocks to don’t be able to block hits from behind him, however, Instead of doing that, I made it check if am I looking towards him when I hit or not. Currently, it works like this: I look towards the player while hitting and it lets him block, I rotate my back towards him and he can’t block. I’m not sure how to explain that, here’s a video of how it looks like right now: https://gyazo.com/cdecb6057ac79b9bad7d9d3f8c89a93a
Here’s my current script:
local Facing = mychar.HumanoidRootPart.CFrame.LookVector
local Vector = (hitchar:FindFirstChild("Head").Position - mychar.HumanoidRootPart.Position).unit
local angle = math.deg(math.acos(Facing:Dot(Facing)))
if angle > 300 or angle < 60 then
print("block")
...
else
print("hit")
...
end
I tried to change it to this
local Facing = hitchar.HumanoidRootPart.CFrame.LookVector
local Vector = (mychar:FindFirstChild("Head").Position - hitchar.HumanoidRootPart.Position).unit
local angle = math.deg(math.acos(Facing:Dot(Facing)))
However, it did not work, I’m not sure how to do that.
local maxHitAngle = 45
local humnCF= mychar.HumanoidRootPart.CFrame
local left = (humnCF*CFrame.Angles(0, math.rad(maxHitAngle ), 0)) * CFrame.new(0, 0, -0.1)
local right = (humnCF*CFrame.Angles(0, math.rad(-maxHitAngle ), 0)) * CFrame.new(0, 0, -0.1)
local fartherLeft = (humnCF*CFrame.Angles(0, math.rad(maxHitAngle + 0.1), 0)) * CFrame.new(0, 0, -0.1)
local fartherRight = (humnCF*CFrame.Angles(0, math.rad(-maxHitAngle - 0.1), 0)) * CFrame.new(0, 0, -0.1)
local headPos = hitchar.Head.Position
local isInsideAngle = (headPos - left.p).Magnitude < (headPos - fartherLeft.p).Magnitude and (headPos - right.p).Magnitude < (headPos - fartherRight.p).Magnitude
if isInsideAngle then -- the angle might be negative instead of positive depending on what you want, so just put "not isInsideAngle" if you need it reversed
print("block")
else
print("hit")
end
I’m not sure why you’re getting behavior that is dependent on which direction your character is facing, but from looking at your script, there’s a few things I’ve noticed. You’ll probably want to check these things just to make sure they’re not the source of the problem:
I’m pretty sure you meant to put Vector:Dot(Facing) but put Facing:Dot(Facing). I’m not entirely sure why this would ever allow you to hit the character because that angle should always be 0, but it’s definitely not what you intended to write.
You’re using the head of hitchar and the HumanoidRootPart of mychar to get the “hit direction”. This causes the vector to angle upwards towards hitchar’s head and could cause an attack to meet the criteria for a hit even though it comes from in front of the defending player. You’ll want to use the same part from both characters to ensure this won’t be an issue.
If it isn’t either of these two things, the problem lies deeper, but it’s best to get the simple stuff out of the way first.