this is the code that i have for the healthbar, what do i need to add to get it to display only when looking at the enemy?
thanks in advance <3
Could use a line or sphere trace from the camera,and if it hits something, call a function on it to display the health bar. If you use a blueprint interface to setup the function, you can use interface message to call the function without casting to the actor.
Something like this in a function run in tick on the player:
(Ignore the vector out, I’m using that for something else. and sorry for the noodley mess)
Then in my Character Interface, I implemented this as a function on NPCs
Might need some validity tests in there somewhere. I threw it together in like 2 min.
thank you for your help, but im honestly kinda clueless and really dumb.
Should the Line Trace be called after my code or as a new function in the beginning? And how do you cast a trace from the camera? and i also havent worked with interfaces before, so i didnt now and i need a target but obviously cant cast to it since its in the ai logic. So is there a way for it to work without Interfaces?
thanks
Line trace would be called in the tick event on the player char.
Here’s the documentation on how to do a trace.
When a trace hits something, it will return a whole bunch of info, and you can use the HitActor output as a target for casting, interface messages, or pretty much anything that requires an actor ref.
Interfaces are intimidating at first, but are actually pretty easy to setup:
After making it once, you can implement it on any other class that might need the functions later (like if you want to destroy walls or something).
To do it all in the ai logic could get expensive, since every npc that’s loaded would need to get the player camera and do traces to check if player is looking at them. Running the trace from the player, and only “pinging” enemies the trace hits to tell them to show health would be much faster.
Alternatively, if the goal is to just show health of actors the player is in combat with, the cheapest way to do it would be to set the health widget visibility to hidden on BeginPlay, and set it backto visible when they take damage. Then use a timer or retriggerable delay to hide it again after a few seconds of not taking damage. That’s how I’ve been doing it in my third person game project, I kinda just duct taped the show/hide function above onto another part of my code.
Currently, I call this event any time my npcs take damage or attack:
So the healthbar goes away after 6 seconds of inactivity. Main downside to doing it like this is healthbars show up if two npcs are fighting each other or one falls off a roof or something and takes fall damage. And it wouldn’t work well with multiplayer, but is ok for a single player game.
Thanks for your Reply. The code you provided at the bottom is not really what im looking for so i tried to do your first solution again, now bear with me.
I setup the function in an Interface.
Then in my TPSCharacter Blueprint i made the function, but i dont know, is Viewtarget just supposed to be a Variable or what is it?
Then i call it after Event Tick in my Event Graph (do i need to hook up Hit Actor or Location with anything?)
And lastly and the most confusing part my dumbass brain cant understand, you said to implement this code in your Character Interface, but Interfaces are Read Only so i directly created this as a function in my NPCs. And for the Input and Output on the Return Nodes, I had to add them manually, so i dont know if did something wrong there
The view target above was a variable of type: actor. The reason for storing as a var is so that it can send the hide message to the last seen target when the line trace fails.
Outputs on the line trace function don’t necessarily have to go anywhere. In my case I also use the trace to scan for interactive props like doors and furniture, so I use the outputs to get interaction type via interface and the location for a moving character to a chair or whatevs.
If all the interaction is happening inside the trace function, you don’t even need outputs on it really.
As for the interface being read-only, that’s just one of the quirks of the system. Once you implement the interface onto an actor in its class settings, the empty function will show up in the details panel of bp editor where you essentially override it like the native events or functions. Once added, the inputs/output pins from the interface class should show up automatically.
The doc page for implementing interfaces is here
Got it working now, the Interface not being added to the Class was the issue.
Thank you so much for your help and patience dude <3
edit: it works and im happy but for some reason if i look at them for longer than a second the healthbar stays there permanently, any ideas maybe regarding that?
The target for the Show health message must not be getting cleared properly on non-hits from trace.Try adding some PrintString nodes to the trace function to see what it thinks you should be seeing. If using a var to remember last known actor, it’s important to only clear the var (by setting to null) after sending the hide message.
This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.