How to make a counter.

Hey,
I want to give cars in my project a velocity meter. I don’t want to calculate the real speeds they are driving, so I thought about making a script that does this ‘fake’. I want to make a counter that counts to any number (variable) in something from 2 till 5 seconds when you press W. But when the number of the counter is equal to or larger than the given number, it will stay at that number, till you release W. I have a good Idea of how to write this script, but I don’t know how to make the counter actually count. I had the idea to do it with function update(), but then the number will eventually just change into the given number immediately.

Can someone please help me on how to make this script work and make it count?

You have the right idea to do this in Update.

If ‘w’ is pressed, raise the value.

If ‘w’ is not pressed, lower the value.

At the end of every Update, use Mathf.Clamp to make sure the value stays within bounds.

Don’t forget to use Time.deltaTime.

(Not to be rude daniel) and no offense, but W seems like he is a little new to scripting and needs a simpler explanation.

I can help you out, what language are you using to make the counter?

(Not to be rude BondoGames :slight_smile: ) and no offense, but I’m of the opinion that it is better not to spoonfeed those who are trying to learn.

I’ve split the task into multiple smaller tasks, and provided a couple of tips. If TheWgames still can’t figure it out s/he has only to post here again to ask about the specific part/s that are posing a difficulty.

Sorry just thought it would be better to simplify.

In JS I would approach this task by this:

#pragma strict

public var currentSpeed : float = 0; /* The vehicles current speed */
public var maxSpeed = 126; /* The Vehicles max speed */

public var DebugMode : boolean = true; /* If the console will show Debug Logs created */

function Start () {
	currentSpeed = 0;
}

function Update () {
	if(currentSpeed > maxSpeed) { /* If you went above the max speed, slow the car down */
		currentSpeed = maxSpeed;
		if(DebugMode == true) {
			Debug.Log("Player is above max speed, putting speed down.");
		}
	}
	
	if(currentSpeed < 0) {
		currentSpeed = 0;
		if(DebugMode == true) {
			Debug.Log("If the vehicle is below 0 speed, put it back to 0");
		}
	}
	
	if(Input.GetKey("w")) { /* If holding W up the speed */
		currentSpeed = currentSpeed + 0.5;
		if(DebugMode == true) {
			Debug.Log("Playing is Accelerating");
		}
	}
	
	if(Input.GetKey("s")) { /* If holding S down the speed */
		currentSpeed = currentSpeed - 0.8;
		if(DebugMode == true) {
			Debug.Log("Player is slowing down");
		}
	}
	
}

function OnGUI() {
	GUI.Box (Rect (0,Screen.height - 30,200,30), "Speed: "+currentSpeed.ToString("f0")+" / " + maxSpeed); /* Draw a GUI that will round current speed to the nearest number as to not show the decimals, then show what the max speed is */
}

The entire thing is commented for your educational use, I know Daniel that we need to give them tips, but however looking at some commented code, will allow them to see how things work, and begin to write it themselves, as long as we aren’t giving away any advanced code, like a Inventory system or the actual physics of a car.

I commend the effort BondoGames, however there are a few changes I would make to your script, as well as a few changes that need to be made in order for it to work (you didn’t use Time.deltaTime). I also removed the debug variable as a matter of preference.

#pragma strict
 
public var currentSpeed : float = 0; /* The vehicles current speed */
public var minSpeed = 0; /* The vehicles min speed */
public var maxSpeed = 126; /* The vehicles max speed */

public var accelerationSpeed : float=10f; /* The speed the vehicle accelerates/second */
public var decelerationSpeed : float=10f; /* The speed the vehicle decelerates/second */
 
function Start () {
    currentSpeed = 0;
}
 
function Update () {
    
    if (Input.GetKey ("w")) { /* If holding W up the speed */
        currentSpeed += accelerationSpeed*Time.deltaTime;
    }
    
    if (Input.GetKey ("s")) { /* If holding S down the speed */
        currentSpeed -= decelerationSpeed*Time.deltaTime;
    }

    currentSpeed = Mathf.Clamp (currentSpeed, minSpeed, maxSpeed); /* Keep the speed within minSpeed and maxSpeed */
    
}
 
function OnGUI() {
    GUI.Box (Rect (0,Screen.height - 30,200,30), "Speed: "+currentSpeed.ToString("f0")+" / " + maxSpeed); /* Draw a GUI that will round current speed to the nearest number as to not show the decimals, then show what the max speed is */
}

I was aware of not including such things, I didn’t want to hand over an advanced script to a beginner, rather allow him to figure out solutions to my basic script himself :slight_smile:

There’s nothing advanced in the script I posted, it’s a simplified/fixed version of the script you posted.

Handing over a script that intentionally does not work (without letting them know it does not work) is not a very good way to teach.