- Home /
How to give points after certain number of seconds?,How to give points after a certain number of seconds?
Hi everyone! So basically i made the whole game and its almost ready, but one of the few scripts that i need is a score script, but unlike other little project i made, this one is making me struggling for almost a week now..what i want is this: Make a script where once the player starts moving, it starts a timer, and every three seconds, if the player has not died yet, it recieves 10 points.
Now, i have tried with coroutines, while loops..but nothing seems to work? Maybe i'm writing something wrong...i really hope one of you can help me out with this one!,Hi everyone! So i basically made the whole game (i'm a beginner but with a little bit of experience, still learning tho) and the only script that i need to finish the beta is a score script... its almost a week i'm struggling make a script like this:
Make a script where when the player starts moving, it starts a timer. And every three seconds, if the player has not died yet, it gives 10 points.
Now, i tried with coroutines, while loops...but nothing seems to work! Hope some of you can help me out with this one :)
Answer by highpockets · Apr 13, 2019 at 07:35 AM
You were correct by using coroutines, but there may have been an error in your logic. Here is an example for your situation:
//I don’t know how you get your player movement, so I’ll put a bool that’s false until his initial start position is different from his position
bool playerMoving = false;
bool countPoints = false;
Vector3 startPos;
GameObject player;
void Start(){
player = GameObject.Find(“PlayerName”);
startPos = player.transform.position;
}
void Update(){
if( player.transform.position != startPos && !countPoints){
playerMoving = true;
}
if(playerMoving){
StartCoroutine(PointCounter());
countPoints = true;
playerMoving = false; //set to false to ensure you don’t start multiple coroutines
}
}
int pointCounter;
IEnumerator PointCounter(){
while(countPoints){
yields return new WaitForSeconds(3);
pointCounter = pointCounter + 10; //add to score
}
}
Hope that helps
hi...first of all thank you for your kind help! :) ...so..what worked was the initial timer. What didn't work was the coroutine beacause it gives me an error with every line that has "countPoints" in..dunno why tho...basically my problem is that the player dies when a sort of arm rotates more than a certain angle, at that point i thought that something like this could work: (note its not the actual code)
bool IsAlive; bool IsDead;
in the start function i putted: GetComponent; to get the rotation values in the update:
if(transform.rotation<80 or <-80) { bool IsAlive = true; }
if(IsAlive) {
//here i would put the IEnumerator for the point count and the timer. But it doesn't work for shit.
}
if(transform.rotation>80° or >-80°) { bool isDead = true; }
if(isDead) { //game over shit and all that, it stops to count points }
also, i know i can't get the rotation axis values (its a 2d game) like this...and i searched a lot, and just found a lot of complicated and poor explains about quaterions...i don't need that much complicated shit, i just want to know what angle my player's arm is... :(
I changed my code. I accidentally declared cointPoints twice.
Your answer
Follow this Question
Related Questions
How do I do I time-base score script 0 Answers
Pause Time.time? 1 Answer
How do I code time based score unity3d 3 Answers
3 star time base system 0 Answers