Odd pause when firing bullets

Hi guys and gals,

I have an issue that when I fire a round from my spaceship, there is an ever so slight pause… this can be a little annoying when firing off loads of bullets in quick succession. Also, for some reason when testing on my old G4 eMac, when I fire the FIRST bullet, there is a huge pause, but it then runs fine after that…

I’m a bit of a Unity noob and it’s probably something basic, but any help would be greatly appreciated :wink:

Cheers!

Do you can post your fire code?

Are you instantiating bullets?

Hi,

Yes, I am instantiating the bullets:

var speed = 40.0;
var bullet : Transform;

function Update ()
{

var x = Input.GetAxis(“Horizontal”) * Time.deltaTime * speed;
var y = Input.GetAxis(“Vertical”) * Time.deltaTime * speed;
transform.Translate(x, y, 0);

if (Input.GetKeyDown(32)) {
Instantiate(bullet, transform.position, transform.rotation);
}
}

note: (It doesnt solve your problem) you must add some velocity to the bullet.

var bulletspeed : float = 60;
var bulletshoot : Transform; //Or Rigidbody
bulletshoot = Instantiate(bullet, transform.position, transform.rotation);
bulletshoot.velocity = transform.TransformDirection(Vector3(0,0,bulletspeed)); //bulletspeed is one word

another note: use FixedUpdate instead of Update

That is constant.
so Update gives you 20 fps then 2 fps then 100 fps
this is not so good.
FixedUpdate gives you always for eg. 20 fps

and you can control your shoot frequence:

var frame = 10;
var every = 0;

function FixedUpdate {
if(every>=frame) {
//Do something usefull…
every = 0;
}
every++;
}

That do something each 10 frames.

realm_1

Thanks for that. I have the bullet speed set up already on a separate script attached to the bullet. Should I not do this? Still not quite sure why I’m getting the pause… odd…

Ah, problem solved… seems it’s a CPU issue… runs sweet on my new PC but the Mac is just a little too old and slow and my placeholder gfx have a fair few polys.

ok
I’d place the speed in the same script but you can do that also in your way.

I’d look into … bah forgot the name.

Basically you have an array of 50 (or whatever) premade bullets, hidden off map, so as to avoid continual instantiate/destroy costs. You can make it more complicated as you wish.

Done a lot in Flash, but can be advantages in unity - someone recently posted a script for it.

If someone can remember the name…

instantiation can introduce small hiccups. Load some bullets into the scene and keep them off camera and move them in when you need them. That way ram is preallocated so there is no hiccup on the fly.

This concept is called a Pool and it’s indeed quite commonly used in Flash games. Every managed environment (i.e. with a garbage collector) like Unity’s benefit a lot from this kind of architecture : you spare resources necessary for instanciations AND garbage collection.

Cool, I’ll have a go at that, thanks :slight_smile: Used arrays a bit in C++ (currently studying that), so should be able to work it out… hopefully. LOL.