How to detect which side of the enemy i am

  1. What do you want to achieve?
    I want to make it so when i hit a target player, i can detect which side i hit him from, his left or right. So i can play appropriate animations like the ones i have here:
    image

Now the problem is, is i’m really bad at math and if anyone can give me a rough idea how to calculate this, that would be great. Thank you.

Get the delta vector delta = (Enemy.Position - My.Position)

Then do

local newDelta = Enemy.CFrame:Inverse() * delta. . The next part can be a bit annoying. I’ll write out one case: `

local newDeltaAbs = newDelta:Abs()
if newDeltaAbs.X>= math.max( newDeltaAbs.Y,newDeltaAbs.Z) then 
  if newDelta.X >= 0 then 
    -- On the right side
 else 
    -- On the left side 
  end 
end 


Repeat the above for Y, Z, X and Z, X,Y.

1 Like

So wait, what do you want me to repeat?

I don’t want you to repeat anything :), but I’m happy to hellp you out further.

So for the right and left, it isnt working… Here is the code:

local direction = (TargetHumrp.CFrame.Position-originRootPart.CFrame.Position)
	local newDelta =  TargetHumrp.CFrame:Inverse() * direction
	
	local newDeltaAbs = newDelta:Abs()
	if newDeltaAbs.X > math.max(newDeltaAbs.Y, newDeltaAbs.Z) then
		if newDelta.X > 0 then
			print("Right")
			
		else
			
			print("left")
		end
	end

When i go to his right side, it prints left, and it doesnt print anything when i go to his left side

Told you a sign was probably wrong. Change the first two lines to the following 1 line:

local newDelta =  TargetHumrp.CFrame:Inverse() * originRootPart.Position

If a sign is wrong, just swap the prints.

Okay, good news, it works, only if im directly on the left and right side. Do you have an idea on how i would do this? :
image
So the arrows show where it should indicate right, and on “Same Here” should indicate left, what i mean is, the dividing line, when you cross over it, it should indicate right / left

You have to add the other 2 parts I put in my second post.

image

Alright i added the 2 points you wanted me to add, now for me to detect the thing i asked you how to do, what should i put here as well?

I guess make a table of the animations

local tab = {
  ["left"]   = animationTrackForLeft;
  ["right"] = animationTrackForRight,; ... 

Then play the appropiate track, i.e. instead of print(“left”) do

tab["left"]:Play()

Here’s a function I used to check which direction the player is facing

function IsFacing(cframe : CFrame, position1 : Vector3, position2 : Vector3, face : string) : boolean
	local difference = position2 - position1

	if face == "Front" then
		local lookVector = cframe.LookVector
		
		if (lookVector:Dot(difference) > 0) then
			return true -- The player is in front
		elseif (lookVector:Dot(difference) < 0) then
			return false -- The player is behind
		end
	elseif face == "Right" then
		local rightVector = cframe.RightVector
		
		if (rightVector:Dot(difference) > 0) then
			return true -- The player is on the right side
		elseif (rightVector:Dot(difference) < 0) then
			return false -- The player is on the left side
		end
	end
end
local isFront = IsInFront(rootpart.CFrame, rootpart.Position, enemy.Position, "Front")
local isRight= IsInFront(rootpart.CFrame, rootpart.Position, enemy.Position, "Right")
1 Like