How can I fully control timimg within unity? (Rate of fire etc)

Hi,

I've searched around for an answer for to this question for a few days now and with few other bugs in my proof of concept left to fix i thought it was time to ask for some help. I've been using unity for about two weeks now and this is my first post on the forum.

I have a few problems that relate to timing, it seems really stupid but I can't seem to measure time in anything smaller than a second. This is causing my AI problems, (it is a very simple turn towards player and shoot script) they shoot their maximum number of shots per second very quickly and if shots are fired a moment before the clock ticks to the next second more bullets are fired in quick succesion. I would prefer that if I set them to 10 shots per second that 1 shot is fire every 1/10 of a second.

Another issue is that because they are set to update their potential target once per second using the Physics.SphereOverlap (or something like that) and they do it on the first frame after the clocks ticks over to the next second it means they all do that on the same frame, this isn't an issue until you have 100 or so units on the screen and then once per second framerate drops massively, I'm only guessing this is the cause but it seems likely.

I have a similar problem but a little easier to explain pasted below and if I can solve this I think I can solve all my timing problems, this is part of my bullet's script, it seemed the simplest way to destroy my bullets were to give them a lifespan since the screen boundries move there were no fixed boundries that could mark their destruction and I figured measuring distance from origin would be a lot more intense processing than simply giving them a timed life. The Time.time command that I used only updates once per second so if I fire many bullets over a period of a couple of seconds they disappear in groups rather than individually which is exactly what you'd expect from the code, but I was hoping there is someway to measure 100ths of seconds or even 10ths.

var bulletLife : int;

function Update () {

if(!bulletLife){ bulletLife = Time.time; }

if(bulletLife < (Time.time - 3)){ Destroy(gameObject); }

}

Any pointers would be really appreciated. Thanks for even reading my question.

Dan

In your case you could do something like:

Start() {
    Destroy(gameObject, bulletLife);
}

instead of having to check every update (especially if you're working on iphone where CPU cycles are scarce. Another way to handle things at certain moment in time is using Coroutines, for example, alternative solution to problem would be:

function Start() {
    COBulletLife();
}

function COBulletLife() {
   yield WaitForSeconds(bulletLife);
   Destroy(gameObject);
}

Your problem is that bulletLife is an int. Make it a float. The precision will be a lot better than even 1 100th of a second.