Hi
How to make a simple count from 0 to 5. ? when i press SPACE. 0 1 2 3 4 5 ?
Hi
How to make a simple count from 0 to 5. ? when i press SPACE. 0 1 2 3 4 5 ?
Check out a for loop.
Or use more words to explain what you mean. Is there a Text on screen that should show the numbers 0 through 5, one at a time, 1 second apart? Or do you have six objects you need to assign a number to? Or one of the dozens of other scenarios that would fit your question?
The answers to these different scenarios will be quite different.
counting man. simple count. from 0 to 5. “0,1,2,3,4,5” count in seconds while I am pressing C.
int number = 0;
if(Input.GetKeyDown(KeyCode.C))
{
// count number up
if ( number >=5) {
number = 5;
}
Debug.Log(" counting up " + number);
}
else {
// count number down
if ( number >=0) {
number = 0;
}
Debug.Log(" counting down " + number);
}
OK, I think you didn’t understand my point — there are many different ways and things to count. But you’ve now given enough clues to guess more or less what you mean anyway.
Here’s how I would do it.
Add a value field to your class. IMPORTANT: this must be a float.
In your Update method, use Input.GetKey (not Input.GetKeyDown) to check whether C is pressed.
If it is, add Time.deltaTime to your value.
Else, subtract Time.deltaTime from your value.
Display (somehow — I doubt Debug.Logs are what you really want) the value, truncated to an integer (for example, with Mathf.FloorToInt).
thank you Joe. its work now. here is my script:
public float number = 0;
void Update() {
if(Input.GetKey(KeyCode.C))
{
number += Time.deltaTime;
if ( number >=5) {
number = 5;
}
Debug.Log(" counting up " + Mathf.FloorToInt(number));
}
else {
number -= Time.deltaTime;
if ( number <=0) {
number = 0;
}
Debug.Log(" counting down " + Mathf.FloorToInt(number));
}
}
If there is anything can i add to improve the code please write it.
Thank you so much.
Looks reasonable to me.