Is it possible to use WaitForSeconds method to wait for value lower than 1 hole second?
WaitForSeconds method does take a float, but it acts like it is working with integer;
wait for seconds works for input smaller than 1 second yes. But you probably might have choosen one that small that it could no longer work or you have set a target framerate of 1 so update only happens once per second and so all wait / yield is only evaluated once per second either
ty for response.
I didn’t set any framerate manually. if i put Debug.Log(Time.time); in my update method i get values like 0.03236212, 0.09977312, 0.1066772…, so i guess update method is called more often than every 1 second.
but even if i send value greater than 1, like 2.5, it acts like i have sent 2.
use a time stamp instead if you want more accurate control e.g.
if(Time.time-timeStamp > 2.5){
//do something
}
just set timeStamp to Time.time when you want the timer to start
can you provide the offending code?
tnx for advice. I was hoping to avoid calculations with timers and use corutines, it seems more professional, but i guess i will use timers.
ah, yes, sure. i was replaying to spinaljack
public float _moveSpeed = 10.0f;
private Transform _transform;
private CharacterController _controler;
private bool IsMoveEnabled = true;
public float _shootWaitTime = 0.5f;
// Use this for initialization
void Start () {
Debug.Log(Application.targetFrameRate);
//Application.targetFrameRate = 300;
_transform = this.transform;
_controler = this.GetComponent();
IsMoveEnabled = true;
}
// Update is called once per frame
void Update () {
if(IsMoveEnabled)
{
if(Input.GetButtonDown(“Fire1”))
{
IsMoveEnabled = false;
Debug.Log("Starting corutine at: " + Time.time);
StartCoroutine(WaitForShoot());
}
else
{
Vector3 moveDirection = new Vector3(Input.GetAxis(“Horizontal”), Input.GetAxis(“Vertical”), 0);
moveDirection = _transform.TransformDirection(moveDirection);
moveDirection *= _moveSpeed * Time.deltaTime;
_controler.Move(moveDirection);
_transform.position = new Vector3(_transform.position.x, _transform.position.y, 0);
}
}
}
IEnumerator WaitForShoot()
{
yield return new WaitForSeconds(_shootWaitTime);
IsMoveEnabled = true;
Debug.Log("Corutine finishes at: " + Time.time);
}
hmm, see no problem here, should technically work
question is did you change shootWaitTime in the editor?
omg what a noob i am
i thought it will change to default value from script.
ty verry much for help
hehe
the default value in script only impacts the initial value. if you later on want to override the editor value you will need to do so in Awake or Start
That’s not any more accurate than WaitForSeconds.
–Eric
Both of these methods seem pretty inaccurate for me. I wanted to put together a simple sprite animation class but I’m getting frame rates that are at least 3 to 5 fps slower then the set frame rate. This is a around 20fps. As I increase my target frame rate the actual frame rate accuracy gets even worse. Does anyone know a more accurate technique? I’m running the two methods in a coroutine, here is the source:
IEnumerator RunAnimation(int startFrame)
{
float frameTime = 1/frameRate;
currentFrame = startFrame;
int lastFrame = frames.Length-1;
float timestamp = Time.time;
while(true)
{
float delta = Time.time - timestamp;
actualFrameRate = 1/delta;
timestamp = Time.time;
renderer.material.mainTexture = frames[currentFrame];
if( currentFrame++ >= lastFrame )
{
currentFrame = 0;
playing = false;
if ( loop ) Play(0);
break;
}
yield return new WaitForSeconds(frameTime);
}
}
IEnumerator RunAnimation2(int startFrame)
{
float frameTime = 1/frameRate;
currentFrame = startFrame;
int lastFrame = frames.Length-1;
float timestamp = Time.time;
while(true)
{
float delta = Time.time - timestamp;
if( delta >= frameTime )
{
timestamp = Time.time;
actualFrameRate = 1/delta;
renderer.material.mainTexture = frames[currentFrame];
if( currentFrame++ >= lastFrame )
{
currentFrame = 0;
playing = false;
if ( loop ) Play(0);
break;
}
}
yield return null;
}
}
Have you tried InvokeRepeating?
–Eric
Well! I feel stupid for not thinking about that. The first 31 calls to the step method using InvokeRepeating gave me crazy frame rates (things like “Infinity”, and “1363.588”), but from 32 on I was getting values on the nose (things like “25.00002”, and “24.99973”). Any idea why the beginning is so off? Since I am controlling an animation it needs to be pretty close the whole time.
public class AniTimer : MonoBehaviour
{
public float framerate = 25.0f;
float timestamp = 0;
[ContextMenu("Begin")]
void Begin()
{
float rate = 1/framerate;
InvokeRepeating("Step", 0, rate);
}
void Step()
{
float delta = Time.time - timestamp;
float rate = 1/delta;
Debug.Log(rate.ToString() + "\n");
timestamp = Time.time;
}
}