- Home /
Question by
NikkF · Jul 11, 2019 at 06:44 PM ·
playerplayer movement
How to detect if the player is idle
Hello everyone,
I'm making a horror game and using the standard unity fps controller i want to detect if the player is AFK for 2 minutes so a sound can be played.
How do i go about doing this without changing code of the fps controller if possible?
Thanks! Nikk
Comment
Best Answer
Answer by Shadowing · Mar 11, 2020 at 08:48 AM
Here is a much cleaner way of doing this
int IdleTimeSetting = 60;
float LastIdleTime;
void Awake() {
LastIdleTime = Time.time;
}
private void Update() {
if(Input.anyKey){
LastIdleTime = Time.time;
}
}
public bool IdleCheck(){
return Time.time - LastIdleTime > IdleTimeSetting;
}
Answer by Kakkapylly · Jul 11, 2019 at 06:52 PM
public int time = 0;
//Use fixed update beacuase its called every fixed framerate frame
void FixedUpdate () {
if(!Input.anyKey){
//Starts counting when no button is being pressed
time = time + 1;
} else {
// If a button is being pressed restart counter to Zero
time = 0;
}
//Now after 100 frames of nothing being pressed it will do activate this if statement
if(time == 100) {
Debug.Log("100 frames passed with no input");
//Now you could set time too zero so this happens every 100 frames
time = 0;
}
}
I was thinking about !Input but i didnt lnow if that would work or break the game. Thanks!
Your answer
Follow this Question
Related Questions
Why does my character vibrates upon starting the game? 0 Answers
Moving and rolling a cube 1 Answer
Player dashes faster in build than editor 1 Answer
glitching player 0 Answers
Player falling through the floor 1 Answer