Rotate player and enemy face to face

Hi everyone, I started learning unreal about a month ago, I made a system that when the enemy has zero life the enemy stuns and I can make a grab to finish him off.

To match the animations I decided to create a bluprint that when the enemy has zero life and the stun animation is active, if I press the dedicated button both the player and the enemy rotate face to face and the animations start. The problem I’m encountering is that when there is a nearby enemy with life above zero and another nearby with life at zero sometimes the player rotates towards the enemy with life above zero.

Does anyone have any advice for me to solve this problem? I am also available to approach different methods, as long as the result is this.

1 Like

Using GetAllActorsOfClass in this scenario seems far too expensive, and it won’t effectively locate nearby enemies. You should use SphereTraceForObjects in front of your player every time you attack. If it hits an enemy, then you can check if it is stunned and call two public functions (not a macro, as you have used in your screenshot), one on the player to make it face the enemy, and one on the enemy to make it face the player.

Thanks for the reply!

I did as you suggested, however, how can I understand that the enemy that the sphere trace is hitting is the same one that put on true can grab event at that moment?

Furthermore, I don’t even understand what to use instead of get all actors of class, because if for example I use cast to enemy and compare the bool can grab if it is true, the print string doesn’t bring me anything back.

GetAllActorsOfClass iterates through every Actor in your scene, which is not feasible in most situations.

I don’t see any print string nodes in your code. Try putting print nodes in different locations after the trace to see where it stops working. Keep in mind that the debug trace will turn green if it hits an enemy. Also, you should replace the == node with boolean AND, and the EnemyFacePlayer function should be defined in the enemy pawn class instead of the player class.

Assuming that you only set EnemyIsStunned to true only when their health is equal to zero, the player should only grab nearby stunned enemies. If you want to grab the nearest stunned enemy, do something like this: How to make ai pick up the closest gun - #2 by anonymous_user_24149e83 (replace the GetAllActorsOfClass node with MultiSphereTraceForObjects and work from there).

1 Like

Thanks again! I managed to do it as you suggested, the only problem I encountered along the way was enemy casting, so I decided to give myself a few days to learn implement the bp interface.

Now on the player side everything works as it should!
If you can, could you confirm that the comments I added are logically correct to detect the enemy hit by the Trace and whether he is stunned?

Looks fine, thought you could remove the top two == nodes and just add more pins to the AND node instead for organization purposes. Also, it’s odd that casting wouldn’t work in this scenario. Assuming that all your enemies inherit from Enemy_Base or a subclass, it should have worked fine. Judging by your previous reply, are you sure you didn’t just accidentally cast an empty reference variable instead of the actual hit actor? Interfaces work fine here, but if all the enemies inherit from the same class or subclasses, an interface is likely not necessary.

Thanks, now I understand where the problem is with the cast, I still haven’t figured out how to make a reference for an enemy spawned by another actor, would you have any advice?

I simply put an actor in map that on begin makes me spawn a quantity of enemies and I created the reference on the spawner.
The problem is that if I try to cast from my main player I still can’t understand which single enemy to do the casting for check variables and more.

appreciate you walking through the solution the way you did!

1 Like

It should work the same way as long as you are finding the enemy with a sphere trace. Just cast the OutHitActor to Enemy_Base and ensure that all your enemies ultimately inherit from that class. That is one of the main tradeoffs of using casting: You need a parent class with all the shared logic to cast to, such as grabbing, health, damage, etc.

Interfaces would work, but it would likely be tedious to define another layer of interface functions to get information about the enemy in this case.

Also, try to avoid storing hard references to actors like enemies in variables, as that can prevent them from being properly unloaded and garbage collected when you destroy them.

Everything works as it should, since I understood how to use the cast I replaced it with the interface to avoid having too many nodes scattered among the actors.

Heres the result.

Thanks again!

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.