- Home /
Question by
recaigeciken14 · Apr 05, 2021 at 08:32 PM ·
timerdeltatime
Unity hand-made timer doesnt work?
When character goes the area, the timer starts just once time but when character goes second time it always stop in zero and not working again
public bool collectable_item;
public float seconds;
private void CharGoing ( string destination )
{
if (transform.position == GameObject.Find(destination).transform.position)
collectable_item = true;
if (transform.position == GameObject.Find("storagearea").transform.position)
collectable_item = false;
}
public void CharWent ( string destination , float secondsTillJobdone )
{
chargoing( destination );
if( collectable_item==true )
{
//checkTimerProproperties = true;
seconds += Time.deltatime;
if( seconds>secondsTillJobdone )
{
transform.position = Vector2.MoveTowards(transform.position, GameObject.Find("storagearea").transform.position, 1.2f * Time.deltaTime);
seconds = 0;
}
}
else
{
transform.position = Vector2.MoveTowards(transform.position, GameObject.Find(destination).transform.position, 1.2f * Time.deltaTime);
}
}
Comment
Answer by andrew-lukasik · Apr 05, 2021 at 10:18 PM
Don't compare object positions with
==
but calculate distance between them
and test for less than
threshold value:
float dist = Vector3.Distance( transform.position , destination.transform.position );
if( dist < 0.1f )
collectable_item = true;
Using
GameObject.Find
is unreliable and slow (the same applies to tags), try to replace it with
direct references to GameObject
s.
[SerializableField] GameObject _destination = null;
Your answer
Follow this Question
Related Questions
I cant get this timer to start when this bool turns true? 1 Answer
timer, corutines, constant deltaTime, metronome like 1 Answer
Accurate timing while using timeScale. 1 Answer
Certain timers run too fast 1 Answer
Timer limits 0 Answers