How to create a metroid prime style lock on system

I’m currently trying to create a lock on system for an fps game that will lock onto any enemy that the player is looking at.

I dont need the ability to change targets as the lock - on only activates while left click is being held.

It would be preferable that this system is designed so that I can just attach something to all the enemy AI (or even non enemies like destructible vents) and then the PC can lock onto them.

This is a camera lock on, so I would like it to take full control of the PC camera and basically make it so you can only circle strafe around the enemy (and move forward and backward)

Possibly important information, my character just has the camera socketed to the head bone.

Ive tried following a few youtube tutorials,one was way too overly complicated and the other one that uses a collision sphere was pretty promising but then it just didn’t work? it was weird. Any and all help is appreciated, resources etc.

Basically ( without me having to write loads of code ) when the player left clicks you need to find the nearest enemy to the camera viewport ( within reason ), which you could do with DotProduct.

You have to do a GetAllActorsOfClass to make an array of enemies, trundle though this array and check the angle between your camera forward vector ( GetForwardVector ) and a vector reaching out to the enemy ( that’s a line trace ). You use DotProduct to find the angle between these vectors.

Once you have the best enemy, you have use LookAtRotation to keep looking at them while the mouse button is down.

It might sound a bit complicated, but it’s not that bad. However you wont get this kind of functionality just falling in your lap… :slight_smile:

Thanks thats actually super helpful, ive been looking everywhere trying to find what the basics i need so that I could research them.

So thank you so much, I managed to get a working loop system that compares all actors with an “enemy” tag, Now im just trying to compare the y vector’s of the targets, Basically I would like the actor with the vector that is closer to 0 to be the one that is assigned as the “closest” I have already managed to get the y vector of the so far “closest enemy” and the current actor that is being checked, but I dont know how to compare them for closest to zero?

If that makes sense.

So I did manage to get a system that gets all the actors tagged with enemy, and then starts to compare them for the closest to the character, but I would like it to compare them based on how close they are to being directly infront of the characters view, and ive managed to get those values but I do not know how to compare them correctly. Have any tips?

Sorry about the comment spam, apparently all my replies had to go through moderators and it took them like 18 hours to get to them, I did manage to find a solution to that problem by splitting the nodes!

I’ve managed to get it working fully! The only thing I am trying to do now is polish it so that when I stop locking on the camera keeps the angle its at, currently it resets to the angle it was using when it first started locking on.

When locked on to an enemy the camera never really centers itself to the enemies capsule. All i know is that I am sending the enemies get actor location to the find look rotation, I would really like to be able to make it more accurate so that it will always lock onto the center of the enemy.

Also the character does not move in a perfect circle? he always kind of moves backwards, more like a spiral. which is bad because when the character does move forward or backward, the focus point of his vision moves down and up so if I were to back away from the enemy far enough I wouldn’t even see it on screen anymore.

Good stuff! The main thing now, is you’re getting the look-at from your camera and then applying it to the capsule, you need to apply it to the camera :slight_smile:

What do you have in that timeline?

Just a looping 1 second float value that is set to the value of 1 at all times… I thought i needed it to get it to constantly apply the set world rotation. Also im applying it to the capsule because when i apply it to the camera the player doesnt always face the target, despite the camera being attached to the head bone. So its just really annoying because the character mesh just moves around freely while the camera is locked onto the target.

Try the look at from your player, instead of the camera…

Dude that helped so much facepalm im a real idiot and should try harder.

Any tips on what I should try to do to get the camera to not reset to the original rotation?

Try this kind of thing: