shooting bullets the right way

This is an aesthetic glitch that has been bugging me for the past 5 months or so. I have a spaceship that shoots bullets. I have a vector3 variable saved in the ship script that tells where, relative to the ship’s position and rotation, to instantiate the bullets, and from there, the bullet script simply moves the bullet forward that a set speed. This works perfectly most of the time. But every once in a while, the bullets will start appearing off to the side of the ship or above it or below it, as if the vector3 variable has been changed (which it hasn’t). The bullets still move in the direction the ship is pointing and function perfectly fine, it just looks bad.
I know im not giving you guys a lot of information to go off of here, but im so tired of this glitch. Any ideas or suggestions would be helpful. Let me know if you need a video of the glitch happening if my description above isn’t enough.
Every time i start a level, the bullets fire fine. It isn’t until the ship has been flying around a bit until the bullets start doing this, and then sometimes it goes back to normal.
I’m building this game for the iphone, but i dont think that would have anything to do with the problem. I considered that the unity engine is rounding some float numbers because my level is very big compared to my ship, but it seems to be behaving more as if the vector3 variable i declared to spawn the bullets at relative to my ship has changed, or the model of the ship is being drawn off to the side of where the ship object really is (which wouldn’t make sense since the camera still follows the ship just fine when this happens).
Before when i was trying to deal with this glitch, i had an invisible object attached to the ship to show where to spawn the bullets at, but i got the same glitch when i changed my code to the vector3 relative to the ship.
Here’s the code that spawns my bullets:
bulletSpawn is the vector3 that tells where to spawn the bullets

Instantiate (bulletPrefab, (thisTransform.rotation * bulletSpawn) + thisTransform.position, thisTransform.rotation);

If no one can figure out what is wrong with my code or technique (i dont blame you if you cant), maybe some of you could share how you’ve made bullets in the past that has worked well (keep in mind, the bullets need to move slow enough for the player to see, so no instant raycasting.)

thanks and sorry for the long post

Are you destroying them when they hit something or get to a certain distance… maybe all these Instantiates and Destroy are causing a Garbage Collection issue that is causing a delay that is stuttering the positioning etc (only guessing here).

When I have to handle bullets I normally create a bunch of them at the start and store them in an array… turn them all off and then turn on and move as required. You may find out that the maximum onscreen at any time is 10, so make your array have 15-20 so you don’t get any probs etc.

Regards,
Matt.

yeah, i have the bullets automatically destroy themselves after a time limit if they don’t hit anything. But including the enemy bullets, there could be up to 20-50 bullets on the screen at one time, and they all use the same script. They all function perfectly fine once spawned though. It’s just where they are spawned that is glitching.
Maybe an array system would help. I’ll look into how to do that.

I dont think it’s a performance issue. I’ve tested it out and i can have the ship shoot at about 800 bullets per second with each bullet lasting 10 seconds before it even begins to lag the frame rate. Even on the itouch 2g, shooting that many bullets only makes it lag a little.
And the spawn placement of the bullets doesn’t seem to change more when i increase the number of bullets. This spawn glitch seems to “turn on” halfway through the level and maybe only a 1/5 chance of happening every time i play a level, and continues until the level is either restarted or continued to the next level.

Hey Dude,

Perhaps this isn’t the best solution but here is what I did in a similar case.

First the vector is not stored in the script, there is an empty child gameobject called bulletOrigin under the player’s ship hierarchy. That object is positioned in the editor so that it is inside the gun port / barrel. Since it is essentially a transform you can place it very precisely in the editor relative to your ‘parent’ ship model.

Now the ship controller / manager script, basically the script that needs to know at which position the bullets need to be instantiated, takes this bulletOrigin game object as a parameter (assigned in editor).

In the manager script bulletOrigin gives you position and even rotation if needed (for some really funky shooting effects). So, instead of relying on your vector variable you have a transform point that is ALWAYS correctly positioned in respect to your ship’s gun ports.

Hope this helps.