Auto Target Switching(Triggers vs Raycast?)

I’m trying to replicate the auto targeting system of a game (Parasite Eve:3rd Birthday).

-Pressing lock on key will automatically lock on to the closest enemy in characters forward direction and enter lock on state.
During Lock On State:
-Pressing down key locks on to closest enemy regardless of facing direction
-Pressing lock on key +left/right will target the next enemy of corresponding direction based on players facing direction.

I’m looking for coding examples/guides and how this might be accomplished.

Trigger method:
-Stores all enemies in a list and their distance inside the trigger. Remove when they are no longer inside the trigger.
-Compares players position and rotation(?) to execute lock on functions above.

Raycast method:
-Cast ray based on player input to quickly scan and snap to the first target found.
-Not sure how targeting closest enemy would work
-Which raycast to use for good accuracy? Spheres/Planes?

I’m not sure I’d use either… if you have a collection of enemies around you, just get their relative bearings (in degrees) and sort them from low to high. Zero would be you are facing it, left negative, right positive.

Then whichever one is closest to zero, that’s the first one you “grab” when you go ADS and lock on.

After that if you ever switch targets, just get the next one up or down in the list according to left or right and say “Target that one instead.”

Obviously the target aimpoint would not instantly snap to the target, but would slew gradually there.

2 Likes