Bullets jitter when ship is at higher velocity

Hello, I’ve found that as my spaceship increases velocity my bullets start to jitter. Higher velocity, the more they all jitter. All bullets jitter at the identical rate/on the same frequency. Bullets fired previously at lower velocity increase their rate of jittering as I increase velocity. At first I thought this was just the float imprecision away from origin, but if I fly back to origin the jittering does not get better. I only modify bullets at a single point to my knowledge, and that’s when I spawn them at time of firing:

Bullet creation code:

// Create a bullet
Vector3 bulletStartPosition = new Vector3(transform.position.x, transform.position.y, transform.position.z+2);
Rigidbody bulletClone = (Rigidbody)Instantiate(Bullet, bulletStartPosition, transform.rotation);

// Accellerate the bullet
Rigidbody gunRB = this.transform.parent.transform.parent.GetComponent<Rigidbody>();
bulletClone.velocity = gunRB.velocity;
bulletClone.AddForce(gunRB.transform.forward * Time.deltaTime, ForceMode.Impulse);

From my limited knowledge, it’s ok for me to modify velocity directly once during creation, right? Any ideas on what could be causing this?

It could be float imprecision being applied to the velocity number rather than the actual position.

I’m confused why you are multiplying Time.deltaTime into your AddForce call. Wouldn’t that make bullets fired during low framerates travel considerably faster than those fired at 100fps? As I understand it you shouldn’t use AddForce anywhere outside of FixedUpdate, where Time.deltaTime is irrelevant.

I notice jittering at a ship velocity of (0,0,40), and the bullets aren’t going much faster than that. So there really shouldn’t be any floating point issues. You’re correct about using Time.deltaTime - I shouldn’t need that inside FixedUpdate(), where this code is.

OK, yeah, 40 is well within the realm where float precision shouldn’t cause issues.

Do the bullets have any scripts on them that could affect their position? Alternately - is it possible that it’s the camera jittering and not the bullets? Since they all jitter together that makes me suspicious. (Keep in mind that the camera and the player’s craft might easily be jittering in unison)

The asteroids I can see through the window don’t jitter, and I did check that my ship controller wasn’t ever touching the bullets. I verified that I have no script on my bullets, and I’m fairly certain nothing is touching them.

well, there’s some ‘interesting’ code :wink:

I dont really see the point of setting the velocity initially to gun rb velocity. Ive never done this on my firing vehicles. The following should be enough.

// Create a bullet
Vector3 bulletStartPosition = new Vector3(transform.position.x, transform.position.y, transform.position.z+2);
Rigidbody bulletClone = (Rigidbody)Instantiate(Bullet, bulletStartPosition, transform.rotation);
bulletClone.AddForce(bulletClone.transform.forward * 50, ForceMode.Impulse);

Also, a better approach (rather than this cluster f***)

this.transform.parent.transform.parent.GetComponent<Rigidbody>();

would be

transform.root.GetComponentInChildren<Rigidbody>();

Well without setting the velocity, my bullets don’t take into account the velocity of my ship (which I’m grabbing off the gun). The ship can be moving quite fast, so it matters.

Haha yeah, forgot I had that cluster in there - hardcoded to get my ship’s hull’s RB while I was testing :).

Anything else I could post to help debug this?

I see no other reason for jitter.

What sort of speeds are we talking?

It’s quite noticable at a velocity of (0,0,40).

Are you sure its the bullets that are jittering?

When I see ‘jitter’ in my game, I know its coming from the camera rather than the objects falling through the sky around me.

I would put some trail renderers on them and see if they trails themselves have jitter or are smooth

Oh cool, didn’t know about trail trenderers. I’m new :). Well, the trails jitter in sync with the bullets. However, the moons and asteroids do not jitter, nor does the ship. I’m not sure what to make of that.

that’s really un-usual behavior. Ive never had a problem like that.

What are your rigidbody settings? Are the bullets children of something? 100% sure theres no other code running on the bullets etc?

1 Like

Yeah, you’re telling me - it’s confusing the heck out of me :). I keep feeling like there’s just something obvious and random I’m missing.

Best I can tell, there is no scripts even accessing the bullets. The bullets themselves are spawned as children of root. I’m away from home right now so I’ll try gather any other code bits and info I can post.

SOLVED! I was getting back at this bullet jittering thing (they kill asteroids now!) and as I was taking an image of the bullet prefab properties to post here, I noticed the RigidBody Interpolate setting. Looked it up, switched it to Interpolate from None, and BAM. No more jittering! Like I said, I’m new :). Thanks everyone for the help!

1 Like