Hi, guys!
I’m currently working on a directional damage indicator, rather than the modern half-circle that appears somewhere in the middle of your screen. Currently, it appears to be taking direction from the world space, rather than local space, relative to your character.
How it’s supposed to work if something is in front of you that’s damaging you (take the Invalid-Texture box as the damage source in this instance.)
How it currently behaves, since the damage source is right of your character:
The left and right indicators should only appear if the thing that is damaging you is directly beside you, off screen. While front and back will only appear if they’re directly in front or behind you.
Here’s my code (apologies for the messy if statements.)
local SourcePos = Humanoid.Parent.DamageSource.Value.CFrame
local CharPos = Humanoid.Parent.HumanoidRootPart.Position
local relPos = SourcePos:pointToObjectSpace(CharPos)
if relPos.X > 0 then
displayDamageIndiactor("Back")
end
if relPos.X < 0 then
displayDamageIndiactor("Front")
end
if relPos.Z > 0 then
displayDamageIndiactor("Left")
end
if relPos.Z < 0 then
displayDamageIndiactor("Right")
end
The function, “displayDamageIndicator()” simply takes a string, such as Left or Right, finds the Indicator that matches that name, and displays it on screen.
3 Likes
Instead of checking whether the character is to the right or left, see which side the direction would be closest to.
local Source = Humanoid.Parent.DamageSource.Value.Position
local CharPosition = Humanoid.Parent.HumanoidRootPart.CFrame
local Relative = CharPosition:PointToObjectSpace(Source)
local v2 = Vector2.new(Relative.X,Relative.Z)
if v2.Magnitude ~= 0 then -- if magnitude is 0, angle can't be calculated
local angle = math.atan2(v2.X,v2.Y)
--use angle to determine what direction
if angle < math.pi*-0.75 or angle > math.pi*0.75 then --infront
displayDamageIndicator"Front"
elseif angle < math.pi*-0.25 then --to the left
displayDamageIndicator"Left"
elseif angle > math.pi*0.25 then --to the right
displayDamageIndicator"Right"
else --isn't any of the previous, so it must be from behind
displayDamageIndicator"Back"
end
else
displayDamageIndicator"Front"
end
Here is an example of this being done:
red part green part.rbxl (15.8 KB)
5 Likes
Going through each if statement like that isn’t desirable in this case, i.e.
if the x position > 0, then it isn’t < 0, but you do both checks, this should be changed to an elseif (not else because that would mean that it would run if x <= 0).
Eg.
if relPos.X > 0 then
--code
elseif relPos.X < 0 then
--code
end
Honestly, to check if it’s directly to the right or left of your character, you just need to make sure that the X axis is 0 (though it isn’t the easiest thing to be directly next to a moving character, so I think you should do 1 to -1 rather than 0). This can be done by using and “if … and … then” statement.
E.g.
if relPos.Z > 0 and relPos.X == 0 then
-- code
elseif relPos.Z < 0 and relPos.X == 0 then
-- code
end
There are lots of ways to approach this, this method just accounts for the position of the player in (what I see) as the simplest way to improve off from your code.
P.S. You spelt “Indicator” as “Indiactor” in your function “displayDamageIndiactor”.
EDIT:
You can also just attach the checking for the Z position to the X position checking, as if both of the X position checks fail, the damage would have to be directly to each side.
Eg.
if relPos.X > 0 then
--code
elseif relPos.X < 0 then
--code
elseif relPos.Z > 0 then
-- code
elseif relPos.Z < 0 then
-- code
end
However, this way goes against my original recommendation of having lee-way with the X-axis position, so I don’t advise this method, though it all depends on how accurate you want the damage indicator to be. You could always add diagonal damage indicators as well!
This is exactly what I was looking for!
However, in the “PointToObjectSpace” portion, you had the Source and the Char flipped around.
Here’s what it was supposed to be, otherwise the arrows would be relative to the damage source, rather than the character:
local Source = Humanoid.Parent.DamageSource.Value.Position
local CharPosition = Humanoid.Parent.HumanoidRootPart.CFrame
local Relative = CharPosition:PointToObjectSpace(Source)
Other than that, thank you for your solution! I appreciate it very much @Halalaluyafail3!
1 Like