x


Time based scoring

Hello I am working on a game, can anyone tell me how to calculate the score based on seconds? For instance if the player plays for 10 seconds the score will be 10; 1 second = 1 point;

more ▼

asked Apr 22 '12 at 01:15 PM

hariprasadh10792 gravatar image

hariprasadh10792
48 8 8 9

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first
var startTime: float;

function GameStarts()
{
   startTime=Time.time;
}

function CalculateScore(): int 
{
   return ( Time.time - startTime);
}

Because time is granular , if you wanted to do you could multiple the result by 10 and have a score that has fractions of a second.

more ▼

answered Apr 22 '12 at 01:33 PM

Fabkins gravatar image

Fabkins
675 15 20 26

I just used the code but it gives me 2 errors! Assets/Scripts/MainScene/ScoreGUI.js(10,14): BCE0044: expecting (, found 'CalclulateScore'. Assets/Scripts/MainScene/ScoreGUI.js(10,29): BCE0044: expecting EOF, found '('.

How do I fix them!

Apr 22 '12 at 01:41 PM hariprasadh10792

I'm not really that sure what @Fabkins is doing here ... for one, he seems to have mixed a bit of C# with JS! You could either calculate the score at the end of a playing session by calling this function:

function CalculateScore () : int {
    return Time.time;
}

or you could increment as you are going if you want like so:

var score : int;

function Update () {
    score = Time.time;
}

Pretty darn simple.

Apr 22 '12 at 02:19 PM Kleptomaniac

Sorry typo.

@Kleptomaniac, no you cant use Time.time on its own as its the time since the program started not the time since the game started.

So, typos asside, my code stands.

Apr 22 '12 at 03:21 PM Fabkins

PS corrected typo. I was having a mad moment and mixing JS and C#.

Apr 22 '12 at 03:24 PM Fabkins

Thanks a lot for that! But when it prints on the screen it even displays Milli-seconds with it, is there a way just to display seconds?

Apr 22 '12 at 03:52 PM hariprasadh10792
(comments are locked)
10|3000 characters needed characters left

It displays mili-second because u have assigned the score avriable as float.

var score : int;

now the scoring will in fixed value

what you can also do is :

var score : int

function Start()
{
   score = 0;
}

function Update()
{
   score = Time.timeSinceLevelLoad;
   Debug.Log("score");
}
more ▼

answered Aug 30 '12 at 08:51 AM

getbiks gravatar image

getbiks
0 1

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1953
x572
x347
x273
x34

asked: Apr 22 '12 at 01:15 PM

Seen: 894 times

Last Updated: Aug 30 '12 at 08:51 AM