Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

directional ray-related GUI question?

Asked by 5 years ago

so, to set this topic up (and i know you really arent supposed to ask questions without some code to go off of beforehand, but ill get to why later), i will go off by saying that i'm trying to make a custom "hurt" hud - in which there are two bars on either side of the screen. what i want to happen is one of these bars will flash when you receive damage from a target relative to what direction the damage came from. so, for example, if there was an enemy to my left, and they shot me, the bar on the left would flash accordingly. anyway, onto the scripting question - i have a vague idea of how i should go about scripting this, which involves the camera's LookVector (which is what most all GUI compasses function off of) and (i think) using the Direction property to draw a ray from the character's torso to the enemy's to get the, well, direction of where the damage came from. i had the idea of adapting off of some GUI compass scripts, but they do not make use of a specific location to tell you what direction you are going, instead using only the lookvector itself. i have little knowledge of how vectors work at this complexity, so any advice would be appreciated here.

1 answer

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

Take the directional vector from the camera to the enemy who shot at you, which is just their position - camera position, take the Dot product of this with the camera's RightVector and you will have a positive value if the shooter is to your right, 0 if they are exactly in the plane formed by the camera's up and look vectors, and negative if they are to your left. For example:

local dp = (Enemy.Position - game.Workspace.CurrentCamera.CFrame.p):Dot(game.Workspace.CurrentCamera.CFrame.RightVector)
 if dp > 0 then
     -- Enemy to the right
 elseif dp < 0 then
     -- Enemy to the left
 else
     -- Enemy is not left or right (they are directly above, in front, behind, etc...)
 end

If you need to know in front or behind, you can do the same thing, just take the dot product with the camera LookVector instead of RightVector. Then a positive value means in front, negative means behind you. If you need to know if they are above, you don't need a dot product if this is normal game where the camera is upright, you just check if the enemy Y coordinate is above or below camera Y.

Ad

Answer this question