- Home /
How can I instantiate more prefabs as the score increases?
Hi Everybody
I was wondering if someone can help me with this problem please. I'm trying to increase the amount of my prefabs instantiated in my game depending on the score that the player has. Here's the script attached to my game manager object:
var INCREMENTpoints : int = 0;
var SCOREdisplay : String = "The score is: ";
function Points() {
INCREMENTpoints++;
if(INCREMENTpoints == 5) {
dropAsteroids.AMOUNTgenerated++;
}
}
function OnGUI() {
INCREMENTpoints.ToString();
var dialRect : Rect = Rect ((Screen.width/2)-200,(Screen.height/2)-200, 400, 400);
GUI.Box(dialRect, SCOREdisplay + INCREMENTpoints);
}
Here's the script that is attached to my main camera:
var asteroid : GameObject;
var gameOver: GameObject;
static var AMOUNTgenerated : float = 1.0;
var isONLYcreatedONCE: boolean;
private var leftRange : Vector3 = new Vector3(-5.139884, 3.581706, -6.911701);
private var rightRange : Vector3 = new Vector3(-5.139884, 3.581706, 7.066976);
function Start ()
{
InvokeRepeating("createAsteroid", 2, AMOUNTgenerated);
}
function createAsteroid ()
{
var randomZ = Random.Range(leftRange.z, rightRange.z);
var asteroidLocation : Vector3 = new Vector3(leftRange.x, leftRange.y, randomZ);
Instantiate(asteroid, asteroidLocation, Quaternion.identity);
}
function Update()
{
if(HealthBar.health <= 0 && !isONLYcreatedONCE)
{
HealthBar.health = 0;
Debug.Log(isONLYcreatedONCE);
Instantiate(gameOver, gameOver.transform.position, gameOver.transform.rotation);
isONLYcreatedONCE = true;
CancelInvoke();
}
}
Thank you for your help.
Answer by NewEonOrchestra · Oct 26, 2012 at 04:30 PM
Why not create a for loop that spawns new asteroids? Say, if the player score is 50,000, divide that by 1000 to spawn 50 asteroids.
for (int i=0; i < PlayerScore / 1000; i++) { //spawn asteriod }
Obviously only call this method on rare occasion, perhaps if the player clears out a level, or maybe on a delay timer so the next wave pops in every 60 seconds... whatever your game calls for
I don't think that is ideal because my current code works fine. If you read my code samples properly you would realize that I don't really need a for loop. I just need something that could update the A$$anonymous$$OUNTgenerated variable in the start function.
The for loop would make them all spawn at once, not what you want I suppose. But the formula I wrote above is pretty much the same.
Why don't you update A$$anonymous$$OUNTgenerated using player score as a numerator over some constant as a deno$$anonymous$$ator, like I write in my psuedocode snippit above?
A$$anonymous$$OUNTgenerated = (some constant)*(player's score)
That makes it a linear function in the form of: f(x) = mx + b .
The higher player score, the more asteroids are called to spawn in your InvokeRepeating method every 2 seconds, as you have it.
Or you could use a quadratic function, the way it takes more XP to get to the next level than the last, present in most RPGs.
By "some constant" I mean a mathematical constant. Dividing x over 45 means that the result is only 1 after x reaches 45.
I'm not entire sure of what you are trying to do, but I think you may not be using InvokeRepeating correctly. The parameters are like this:
function InvokeRepeating (methodName : String, time : float, repeatRate : float) : void Description
Invokes the method methodName in time seconds.
After the first invocation repeats calling that function every repeatRate seconds.
So if you are making A$$anonymous$$OUNTgenerated larger, you are calling the createAsteroid function less frequently, not more.
What I think you want to do is this:
[code] dropAsteroids.A$$anonymous$$OUNTgenerated = INCRE$$anonymous$$ENTpoints / 30; [/code]
This means A$$anonymous$$OUNTgenerated will increase by one every 30 points. But InvokeRepeating is taking A$$anonymous$$OUNTgenerated as a delay between asteroid invocations, so try this ins$$anonymous$$d:
[code] dropAsteroids.A$$anonymous$$OUNTgenerated = 1 / (INCRE$$anonymous$$ENTpoints / 30); [/code]
By dividing 1 over INCRE$$anonymous$$ENTpoints, you are making it so that the higher score you get, the lower A$$anonymous$$OUNTgenerated becomes, which means the more frequently createAsteroid will be called. (At this point you may want to rename A$$anonymous$$OUNTgenerated to asteroidSpawnFrequency or something.
Also, move that equation into an the update method ins$$anonymous$$d of start so the values are updated during gameplay.
Now: I don't know if InvokeRepeating will always look at the current value of A$$anonymous$$OUNTgenerated or the value you passed in when you first made the call at Start. I will try and test this out later.
In the mean time, do you understand the math in all this? It's basic algebra so I'm sure you probably do.
Answer by edrocks4u · Oct 26, 2012 at 10:16 PM
Just change how you increase your score from score++; to a method and call the method instead.
public void IncreaseScore(int i) {
//up your score
score += i;
//instaniate your prefab
Instantiate(gameOver, gameOver.transform.position, gameOver.transform.rotation);
}