- Home /
How do I Create 5 Random Racers?
I'm having a heckuva time figuring out how to create a persistent/permanent group objects that I can select from randomly to race each other over a preset course.
Right now, my racers are primitive spheres (NavMeshAgents). Right now, I can create five or ten spheres and assign them random speed and acceleration numbers and have them race. No problem there. I know how to do all that.
But, at some point, I want to create hundreds of objects and randomly select a few each time to race over one of my courses.
I know how to create the race course and the nav mesh and obstacles and bake them and all that...absolutely no problem with any of that stuff...I already have all that in place and it works fine. I can even manually create the racers and give them random speed/acceleration and have them race...that works fine too.
BUT...
I don't know how to create the List or Class or Group of racers. Once created, their speed and acceleration should remain the same...those should never change. The only thing that changes is that in one race, I might randomly select Racers 5, 17, and 22 to compete. In the next race, I might select 4, 13 and 26.
I've been looking up all sorts of stuff and I can't seem to figure out how to accomplish this.
Answer by ForbiddenSoul · Dec 29, 2016 at 09:09 AM
There are a whole bunch of ways to do this. You could for example have a scrip loop 100 times create a racer, and then add them to something like GameObject array, they will keep their initial properties, and you can just pick them by index number.
// Total number of racers you want
int numberOfRacers = 100;
// An array to store them all in
GameObject[] racers;
void MakeRacers()
{
// This will loop through as many times as you have set "numberOfRacers" to.
for (int i = 0; i < numberOfRacers; i++)
{
racers[i] = // the GameObject of a racer that your other code is making with the random values etc.
}
}
int numberOfRacersToSelect = 12;
GameObject racerToRace;
void RandomSelection()
{
// This will loop through as many times as you have set "numberOfRacersToSelect" to.
for (int i = 0; i < numberOfRacersToSelect; i++)
{
// Get a random float from 0 to the total number of racers you have, and have Mathf.RoundToInt make sure it
// is a usuable integer for our array
racerToRace = racers[Mathf.RoundToInt(Random.Range(0, numberOfRacers))];
// Note that there is nothing in place to stop you from rolling the same random number multiple times.
// CODE FOR WAHT TO DO WITH racerToRace GOES HERE
}
}
Edit: Indexers don't seem to be playing nice with Unity> I would use something else for now.
Thank you too. I'm going to look at what you have immediately. I appreciate you taking the time to help me.
Thanks for the heads up on selecting something twice. I've done this in C# outside of Unity...I'm just struggling with it inside the Unity Engine...I'm missing something basic and It's not clear what it is to me just yet. Outside Unity, I had put in an additional field to indicate if it was selected so it would not get selected twice. Once I figure out how to get all this into Unity, I don't think I'll have a problem selecting anything twice.
I'm still looking at this. I'm able to create the full universe of random racers...I think I'm doing fine at creating any number of these...and I list them up on a GUI to display them. No problem.
However, when I select three at random (or whatever number) I'm having great difficulty in figuring out how to assign one of the racers from the universe to my selection of current racers.
I've been trying a lot of ways to do this but I can't quite get it figured.
Sorry I took so long to reply, I have been busy over the holidays. Glad your making headway.
How is your universe setup? You say you can reference the racers in your GUI, can you not do the same or similar to reference them to your current racers?
I don't know if this will help at this point, but I rewrote the code to account for duplicate random numbers and handled racer selection a little differently.
The code is too many characters to be a comment so here is a link: RandomRacers.cs
Answer by allenallenallen · Dec 29, 2016 at 02:12 AM
How about creating a class with all the information stored as variables? And then have a list of that class saved. Whenever you want to select the racers, just recreate them again from the list.
Something like this?
using UnityEngine;
using System.Collections;
public class Racer: MonoBehaviour // Probably don't even need MonoBehaviour depending on what you do
{
public float speed;
public float acceleration;
// This is where you are making a new car.
public Racer (float s, float a){
speed = s;
acceleration = a;
}
}
And somewhere else, you create a list to store the Racers.
List<Racer> racers = new List<Racer>();
racers.Add( new Racer(4.0f, 5.0f)); // Obviously, you should make a for loop and use random values
And later you can reassign the values to an actual race car GameObject.
float mySpeed = racers[0].speed;
float myAccel = racers[0].acceleration;
Thanks. I'm going to look into your suggestion right now. I asked this question yesterday but I haven't got a chance to look at any answers yet.
Wow. I'm really looking hard at that List idea. Very strong. Occasionally, I would like to eli$$anonymous$$ate some racers and add others. Your list suggestion might be a way to simplify that greatly. It will take me some time to dissect your response. Thanks for your help.
$$anonymous$$y initial impression is that I would almost have to create this class and list outside monobehaviour in a "creation" script and then pull data from the class/list and insert it into my nav mesh agents (which are the racers) in a "selection" script that would inherit from monobehaviour.
...or something like that.
I don't know how to do it other than with two separate scripts like that but my thought is that should be workable. I'll be trying that today as I go along. It will take me some time as I'm working on other things too but I will post something up if it doesn't work.
I'm still looking at this. Haven't forgotten to Accept an answer.
I'm thinking I won't be able to get around inheriting from monobehaviour so I'll need to access these with "AddComponent" etc. But I'm having some difficulty (syntax, or something) in how to code it properly so I can use the racer from the universe pool in my current race.
I've been looking over the documentation for this and doing some searches, but I'm not having much luck as yet in figuring out how to write this.