- Home /
Find gameObject with higher variable
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...
Is the red shell only supposed to work when you're in second place? Assu$$anonymous$$g that's not the case, it's a writing mistake that you said 'lower' rank AND the script is a component of the player in question, you could write something like this (C# code) :
void findTarget()
{
getPlayers = GameObject.FindGameObjectsWithTag("Player"); //Find all cars:
carSettings self = GetComponent<carSettings>();
foreach (var p in getPlayers){
if (p.GetComponent<carSettings>() == self) continue;
if (p.GetComponent<carSettings>().rank == self.rank +1) target = p;
}
If I understand you correctly you just want to find car which is only one position(ranks) higher then you and set that as your missle target? Then after you sort your array get your car index and car one posion higher is just your index + 1. Please correct me if I'am wrong
Hey guys, I've recently switched from GameSalad to Unity so I'm still learning. Can I ask you how did you achieve your ranking method/system? I'm making a car game and that's the last thing I need to complete it. I've researched and tried everything. Can anyone help? I can post any info if anyone is interested in helping me. Thanx in advance.
Answer by jaja1 · May 27, 2015 at 12:41 AM
Hope I understood the question correctly. I like to keep things as simple as possible. So here is just a suggestion (and some pseudo code):
First, create a class that constantly checks for the "rank" of your cars using say, a vector check on Update(). This class should also contain a static collection of all the carSettings components in the game. This is a good time to utilize constructors. When the carSettings component is instantiated with its gameobject, the constructor should add its class to the static collection like so:
public carSettings()
{
RankChecker.AllCarSettings.Add(this);
}
When I say static collection I mean a static Dictionary, List or Hashtable. I prefer these over arrays in most cases because they are "re-sizable". So if you decide to switch to 10 cars instead of 4 its not an issue.
Second, when your "missile" is launched you no longer have to "find" the carSettings components because you can simply call the static collection of the previously mentioned class. Then, you can use a foreach loop to "seek" the correct target from the carSettings class (or any other class) because the collection is static:
int myCarRank;//rank of THIS car
foreach(carSettings c in RankChecker.AllCarSettings/*this is the "collection"*/)
{
if(myCarRank != 1)//if we are ahead no need to fire missiles ;)
{
if(c.myCarRank == myCarRank + 1)
FireMissile();//of course you will implement how the missile's //position is determined
}
}
This works well because there should only be ONE car in the next rank above our own if the rank check is implemented correctly. That is, there should not be two cars in second place simultaneously while we are in third. This sort of idea with the static collection should be used in place of GameObject.Find because it is faster. Assign all your members to the collection ahead of time so you do not have to search for them later.
Also, sorry for the poor formatting. I did not type this up in an editor.