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:
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
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
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? :
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
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")