Lock on system not working on moving target

I have made a lock on system, but when i move the target in game by bumping into it (it is simulating physics) my character keeps looking at the begin position, even if the actor is destroyed or completely somewhere else. This is weird because i’m using the FindLookAtRotation node in a function connected to an Event Tick so it should update the target location right?

Also, when i have two enemies in the scene, i can only lock on one and with the other one the player turns to the enemy once but doesn’t keep looking at it.

I have absolutely no idea what i’m doing wrong. It feels like i have tried everything.

Any help would be appreciated!

Pictures:

Video example:

Thank you! That works. The only problem now is that i can only lock on to one enemy in a scene for some reason.

[…]but when
i move the target in game by bumping
into it (it is simulating physics) my
character keeps looking at the begin
position, even if the actor is
destroyed or completely somewhere
else.

This is because the actor does not really move, the simulation is only moving the mesh. Make the mesh the root of the actor. Or use the mesh location with Find Look At.

The first thing that looks jarring is the loop itself. You’re not really finding the nearest enemy, you’re finding several enemies that are close enough and look at the last one on that list. Also, you set every enemy as the closest one and try to look at the every enemy again.

Wouldn’t it make more sense to find the closest enemy in the loop and only then rotate once? It would look like this or thereabout:

The variables are local so you do not need to Clear anything when calling.

Completely untested but should work fine. Fingers crossed! Do tell if something does not add up.

edit: Not sure how the lock mechanics is supposed to work but perhaps the boolean Lock check should be done before even running this rather costly function - no reason to find the closest enemy if you’re not Locked anyway - or am I misinterpreting how it’s supposed to work? Hard to tell what the goal is. :slight_smile:

edit2: Oh, I think you want to be Locked to the closest enemy providing it’s in the desired range, yesh?

After a while of puzzling i figured it out.

For anyone who wants to have a system like this:

Just as an aside if you wanted a more optimized “nearest enemy” function you could use this node that will do all the work for you and return a sorted array based on distance for you to choose from.

It looks like this: