- Home /
Switching Target during Target Lock
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.
targets.Sort(delegate (Transform t1, Transform t2) { return Vector3.Distance (t1.position, followXform.position).CompareTo(Vector3.Distance (t2.position, followXform.position)); });
By the way followXform is just the instance of the character's transform.
Answer by sparkzbarca · Dec 27, 2013 at 02:33 PM
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
Does spherecasting expensive to do every frame update?
For clarification, my list is sorted by distance from the targets to the players.
However, as per Darksiders target switching system, the list seems to be updated every frame (responsive to the enemy that moves from left to right and vice versa), and if you flick left, you may only select the targets that exist to the left of the selected target. So my guess is that, there are 2 lists created for the left and right sides, sorted by distance from the selected targets to the furthest targets. If this is a possible way to do it, I'd like to get some help with the list generation. Shperecasting is the way to go? Any other alternative?
I don't seem to quit get the second alternative, if that's relevant now, I'd love to explore it.
so the way the second option works is hopefully you understand local space right, which is just a way of making the center of the "world" be that object, so an enemy object that you transform the position of what your saying is
I dont care what it thinks up is I don't care what the world as a whole thinks up is. I want you to put me "the player" as the center of the world and use my personal axes and tell me where he is relative to me.
So for example
i made you a quick example in paint of what that means
https://www.dropbox.com/s/k4pd8sssdf2iako/Screenshot%202013-12-27%2012.49.53.png
so now as you can see the thing is once the word is centered on the player. Then the X axis is the players X axis, its the players left and right, so if in the players local space your position on the X axis is less than your left and if its greater than its right, thats just because of convention of course which is that numbers go up in the right direction and decrease in the left direction, so we exploit that by checking to see if once they've been put in player space an object is left or right.
Heres a code example of that in action with a sorted by distance list.
list SortedList;
vector3 LocalPostion;
foreach(gameobject enemy in SortedList)
{
LocalPosition = player.transform.transformpostition(enemy.transform.position);
//if its X is negative in local space so its left
if( (LocalPosition.x < 0) && FLickedLeft)
return enemy;
//we already know its not on top of the player and its not left so
`` we can just assume its right with an else, if you want to be super secure you could do a check to make sure its positive and he isn't in fact like directly above/below him for some reason in which case just
an else if ins$$anonymous$$d of just an else
else
//he is right so
if(FlickedRight)
return enemy
}
Great would love to try this. How would I get the position of the enemy relative to the player's local space? Any function I can use to do this?
yea, actually i gave it to you slightly wrong in my code but the code is
localpostioin = player.transform.inversetransformpoint(enemy.transform.position)
inversetransformpoint(vector3 world) takes a world position/point and returns its position in local space
so just in that code above put the correct line using inversetransformpoint.
just so you know transformpoint, transformdirection work for positions and direction in local space to go global and inverse takes globals and makes them local
Thanks man, it'll take me a while dwelling into InverseTransformPoint
, this is a new concept for me, will post my finding soon. Thanks again! :)
Your answer
![](https://koobas.hobune.stream/wayback/20220613124859im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Show Top 10 Of 1 GameType 2 Answers
Sorting a list of GameObjects by accessing their int values 2 Answers
Sorting Game Object Name In Numerical and Alphabetical Order via List 2 Answers
Sorting Variables Help 1 Answer