Hey guys,
I’m still kinda wondering about this. I’m currently working on something like a heat seeking missile (similar to a red shell in Mario Kart).
So, at all times, there are 2-4 cars on the track. Every one of those cars has a ‘carSettings’ component with a ‘rank’ variable that shows their current position.
So, say I’m in 2nd position and I’m firing the heat-seeking missile: Obviously, it should find the player in the first position. But that’s where I’m stumbling, code-wise.
I think I’d need to create another array that has all the cars on the track in it and then sort it through the rank variables of all cars - And I know how to do that.
But then how would I find the next higher rank?
Here’s what I’m doing right now:
if(Input.GetButtonDown(LBButton))
{
if(hasRedShell)
{
findTarget();
var spawnedRedShell : GameObject = Instantiate(Resources.Load("Prefabs/redShell"), itemSpawnPosFront.transform.position, itemSpawnPosFront.transform.rotation);
var spawnedRedShellScript = spawnedRedShell.GetComponent(redShell);
spawnedRedShellScript.target = target;
}
And this works nicely. Now, the issue lies in finding the target, which should be the next ‘lower’ rank, so that the target becomes the racer in 1st place when I’m in 2nd place. I tried this now:
function findTarget()
{
getPlayers = GameObject.FindGameObjectsWithTag("Player"); //Find all cars:
var carP1 = getPlayers[1];
var carP2 = getPlayers[0];
var compareRanks = [carP2.GetComponent(carSettings).rank, carP1.GetComponent(carSettings).rank]; //Compare their ranks
System.Array.Sort(compareRanks, getPlayers); //Sort the arrays, so they're connected:
target = getPlayers[0];
}
Which always gives me the highest rank… but obviously, I’d just need to find the NEXT HIGHER rank based on the rank variable in this gameObject.
I hope that made sense - I’m kinda stumbling over the logic and over how to make that query…