I have a working locking targeting system for a third person game and it’s selecting the nearest target from the sorted list when it’s triggered.
However I’m thinking to add a feature where player can switch the target with the right analog stick, so when the stick is push left, the selected target will shift starting from the nearest left of the selected target from the player view and vise versa when the stick is push to the other side. (Much like how Darksiders would)
Can’t figure out how to sort the list.
For the nearest sorting, I’m using delegate with CompareTo wondering if there’s any similar way I can achieve this.
i’d just spherecast off to the side of the player with a reasonable radius and sort the list returned by distance to player and give them the target with the shortest distance.
as long as your not allowing someone to switch to a very distant target this method is just easier and more intuitive.
since you already have a sorted by closest list another possibility is to simply convert each enemy transform position into local co-ordinates using a foreach loop.
that will give you each objects position with 0 on the X axis being the player so
if they flick left go through the list of sorted distance and transform into a sorted local space distance.
then foreach through the list
if the X value of the transform.position is < 0 (so its left of the player) return it
if they flick right
same thing but
if the X value is > 0 return it.
actually jsut one foreach
convert and then dont store it or anything just go through each cause there sorted by distance already
convert to local space
if greater than 0 on X toss it back for right flick
if less than 0 on X toss it back fro left flick