# New to unity, How to score based on time?

I'm completely lost here and need help.

I need help with setting up my GUI so that it will change the starting score of 0 every two seconds based upon weather or not my character (a FPSwalker object named "Racer") is moving. The Text GUI is named Point "Score Current" and to decrease the score by 1 every two seconds that "Racer" isn't moving. I'm at a total loss here. (I'm using Java Scripts)

It's actually not 'Java Scripts'; what you're probably referring to is UnityScript, which is a variant of JavaScript that's specific to Unity (or at least that's my understanding - I use C# myself).

Rather than write a long reply that attempts to address all the different aspects of your question, I'll instead suggest that you tackle this one problem at a time. Choose an aspect of the problem, implement a solution, test it to make sure it's working, and then move on to the next part.

A good place to start might be determining whether the character is moving or not. I assume that you're referring to linear motion here. An easy way to determine if the player is moving would be to consider the player to be moving if a directional key is currently down. However, there could still be cases where the character is in motion (e.g. falling) while no key is pressed. So, you'll need to decide what definition of 'moving' is appropriate for the given context.

If you want a general solution, store the previous position of the character, move the character, grab the new position, and then compute the speed. The code might look something like this (untested):

``````Vector3 previousPosition = transform.position;
// Move the character here.
Vector3 delta = transform.position - previousPosition;
float speed = delta.magnitude / Time.deltaTime;
moving = (speed > speedThreshold);

``````

That's off the top of my head and may not be correct, but it should be something like that.

Then, add some debug output (using Debug.Log()) to make sure that movement is being detected correctly. Once you get that working, you can move on to the next part of the problem.

I agree with Jesse's suggestion to tackle each problem one at a time.

Assuming your character has a rigidbody, you can use rigidbody.velocity.magnitude to test their "forward speed". Then you can add the time of every frame that the character isn't moving, and every full second, the score is reduced by 1.

This isn't complete code - just an idea of where to start.

``````var minSpeedThreshold : float = 0.1;  // minimum speed threshold - to be tuned!
private var idleTime : float = 0.0;   // idle time in seconds (with decimals)

function Update()
{  // if the character is moving slower than the minimum speed threshold
if ( character.rigidbody.velocity.magnitude <= minSpeedThreshold )
{
// add the delta time to the idle time
idleTime += Time.deltaTime;
}

// displayed score is total score minus the whole number of seconds idle
scoreDisplay = totalScore - (int)idleTime;
}

``````

Then you'd use scoreDisplay in your GUI. Tracking totalScore and the penalties might be a useful stat for the player anyhow.

While I've used Unity-specific code examples in my solution, this isn't a Unity-specific problem. I would definitely suggest starting with a simple project, and using the knowledge you gain there to tackle more difficult projects in the future.

Hope this helps.