How do I do a script that is based on how fast the player finish the level for instance, if the player finish the level in 2 seconds, he gets three points. If the player finish the level in 10 seconds, he only gets 1 point.
Beginner in scripting…
How do I do a script that is based on how fast the player finish the level for instance, if the player finish the level in 2 seconds, he gets three points. If the player finish the level in 10 seconds, he only gets 1 point.
Beginner in scripting…
This is the way that I do it (C#, but you can easily convert if needed).
private float elapsedTime = 0; // The amount of time that has elapsed.
private int points = 0; // The player's current points.
void Update ()
{
// Update your timer every frame.
bonusTimer += Time.deltaTime;
}
void AssignPoints ()
{
// Add points based on how much time has elapsed.
// The player gets 2 points for finishing at or under 2 seconds,
// 1 point between 2 and 10 seconds, and no points above ten seconds.
if (bonusTimer <= 2f)
{
points += 2;
}
else if (bonusTimer > 2f && bonusTimer <= 10f)
{
points += 1;
}
}
To use this, you need to decide what prompts the user to get points. At that time, call AssignPoints () to add the points to the score. This is a rudimentary example meant only as a template for you to modify to your liking; we would use consts and avoid the “magic numbers” in the AssignPoints () method, for starters.
An idea that just came to mind is having a function called every second.
Said function would decrease a value for the score.
Like this:
var score = 10; //score starts at 10
function Start(){
InvokeRepeating("Updatescore", 0, 1.0); // calls Updatescore every second
}
function Updatescore(){
if (score > 0){
score -= 1; //subtracts 1 from score until it reaches zero
}
}
// DO THIS WHEN THE LEVEL IS FINISHED:
totalscore += score; //adds level score to total score
NOTE: “totalscore” is the variable for your total score. (should be obvious. )