How to make a block system that blocks only in front of you

Hello guys, I’m trying to make a block system for my combat system.
The issue is that I want to make the block system that block only in front of the player, like if the player got attacked from back, the block won’t block.
I found a similar topic to this but it doesn’t got the answer, so I hope I can get the answer here.

6 Likes

give me a minute ill help you ok?

Alright, Sure. I would be happy.

I use attributes in my combat system, but you can replace the attributes in the code with whatever you use

local vector1 = (rootpart.Parent.Head.Position - char.Head.Position).Unit -- head of attacking player minus enemy head
		local elook = char.HumanoidRootPart.CFrame.LookVector -- enemy look
		local dot = vector1:Dot(elook)
		print(dot) -- optional
		if char:GetAttribute("Blocking") and dot > -0.5 then 
			local blocked = game.ReplicatedStorage.Sounds.Blocked:Clone()
			blocked.Parent = char
			blocked:Play()
			game.Debris:AddItem(blocked, 1)
			return
		end
                 -- from here on do like its an else, so the player isnt blocking/ blocking but they got hit from the back
		char:SetAttribute("Blocking", nil)
                char.Humanoid:TakeDamage(5)
9 Likes

This is how you implement it, i added comments, let me know if you have any questions, by the way, the sound is optional

local dot = vector1:Dot(elook)

I have a question, what is Dot() ?

1 Like

if you want to learn more about Dot() then check out this video, this is how i learnt to use it

2 Likes

Oh, so you used the :Dot() to calculate if the enemy is in front of you ?

1 Like

More like i used dot to calculate if the enemy is looking at you

1 Like

Well, that’s a pretty smart solution not gonna lie. I will notice you if it worked.

The Dot function returns a single number out of 2 vectors.
It basically returns this:
(vector1.X * elook.X) + (vector1.Y * elook.Y) + (vector1.Z * elook.Z)

If you want to learn more about dot product, here’s some useful links about it:

2 Likes

Oh, Alright thank you, I will check those.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.