first, sorry for my bad english and for such long post, just adding as much info as i can, im stuck on this problem for few months now… soooooo…
i know this question were asked and problem raised for hundreds of times by now, but it seems none of solutions from other posts helped me and im kind of having it in unique way, probably… idk…
this video explaining exact issue
i had old version of my game where i moved my spaceship forward with rb.linearVelocity, and rotated ship with rb.rotation = Quaternion.RotateTowards();, moved bullets with rb.AddForce(,ForceMode.VelocityChange).
this way i had no bugs, back then i avoided rb.addForce() because of same bug im having now
i didn’t like spaceship physics when rb.linearVelocity were used to move forward, it felt fake and not fun, it was ok but i want better, so i redo all the code hoping to solve this bug. now i move my ship forward with rb.AddForceAtPosition(ForceMode.Impulse); using single thrust point in the back of spaceship, and rotate it using multiple points simulating real world physics (newtonian) using same rb.AddForceAtPosition(ForceMode.Impulse). basically bunch of small thrusters rotating spaceship, you can see few of them in video, it has particle effect that looks like spray of air in vacuum of space (placeholder graphics)
shooting works by pooling bullets, bullets have trailRenderers as only graphical elements. i add movement force to bullet from weapon script using bulletRb.AddForce() with ForceMode.Impulse,i call function only once where all logic happens - perform bullets pooling tasks and add force to selected bullet, since its vacuum of space, bullet has no drag and just keeps flying on its own afterwards without needing to continuously add force in fixedUpdate()
all works and feels great but as you can see in video, whenever i move forward while turning/steering, my bullets appears to spawn in future position… yeas future, not past, not present, future ![]()
the harder i turn and faster i go forward, the further in future my bullets spawns.
by my testing, it seems this bug happens regardless of method im using for steering, the root cause of problem is adding forward momentum to a ship… just like i said, im using rb.AddForceAtPosition(), but same happens if i do rb.AddForce();, but while using rb.linearVelocity bug disappears together with that very nice feel of physics
///////////////////////////////////////////////////WHAT I TRIED PART////////////////////////////////////////////////////////////
while googling, i found several solutions, i come with some solutions myself, in the end none worked, this is what i tried, at least what i still remember trying:
-
using different unity versions, currently using 6000.0.12f1
-
i tried messing up with bullet and ship rigidbody none/interpolation/exterpolation, tried variations of settings between both rigidbodies.
-
i tried passing ship momentum/inertia to bullet, rb.linearVelocity and rb.angularVelocity, i tested with both at same time and then each individually… even if i have multipliers for inherited linear and angulat velocities, bug persists unfazed. [if lazy no need to read further] ---->
i did like this (bulletRb.linearVelocity = spaceshipRb.linearVelocity * multiplier) in a shoot function that i call only once where i perform pooling tasks and add all forces in question. how i understand for 1 frame bullet is pushed with addforce + inherits inertia spaceship has at that frame, then in next frames bullet behaves according to all the forces it received at first frame…///////////////////////////////////////////////////////////////////////////
linearvelocity seemed to work as should, but rb.angularVelocity did weird thing where each bullet shoots straight how it supposed to, but after certain, seemingly constant/fixed time it changes trajectory with sharp turn. the sharpness of turn gets harder/sharper when i increase multiplier
(bulletRb.angularVelocity = shipRb.angularVelocity * theMultiplier)////////////////////////////////////////
i might be wrong how rb.velocity works, im not sure if inheriting velocity works as i believe it does, need to do testing… either velocity value gets passed at first frame and then actively effects bullet trajectory (bullets holds that velocity value/energy) for remaining lifetime, or it gets passed at first frame, effects bullet trajectory at that frame only, and at next frame velocity(energy) gets lost. -
i tried to do spawn position prediction, basically when pooling, instead placing bullet at a gun position, i calculate different position taking in account player, (or gun barrel) movement speed and rotation, then predicting where barrel will be after set number of frames. tried same but reversed, position where barrel was number of frames ago… system failed, i think im just not that good at coding, even chatGpt was useless generating working code for this.
-
i tried to place pooled bullet to gun location with
-transform.position
-transform.SetPositionAndRotation()
-NetworkTransform.Teleport() //game is multiplayer, netcode NGO
-rb.MovePosition();
-rb.position(); -
i tried several different attempts writing gun and bullet scripts, i deleted all code and tried again several times. i tried adding force to bullet from bullet script, then i tried to do it from gun script or just simply completely restructure scripts…
-
i though maybe somehow when i enable bullet, set its position to gun position and add force, there is some delay or something and its gets enabled to late or to early… so i attempted to preselect future bullet in advance and set its position with SetPositionAndRotation() to gun position inside Update(), and when player finally fires that preselected bullet, i do typical pooling tasks where one is setting bullet position to gun position for the one last time… then i preselect new next bullet and repeat. this way the bullet im going to shoot next is always attached to gun barrel, yet it did absolutely nothing…
//most of my shooting function code, it only gets executed once
//this one is different attempt to calculate not ship angular velocity, but gun barrel
//Vector3 angularVelocityLinear = Vector3.Cross(rb.angularVelocity, currentGunPos - rb.worldCenterOfMass);
//this is quick, dirty and lazy patch while transitioning from gun barrel angularvelocity to ship angularvelocity
Vector3 angularVelocityLinear = rb.angularVelocity;
Vector3 angularInherited = angularVelocityLinear * angularVelocityMultiplier;
Vector3 linearInherited = rb.linearVelocity * linearVelocityMultiplier;
bulletsList[bulletId].SetActive(true); // first enable
bulletsList[bulletId].transform.SetPositionAndRotation(currentGunPos, currentGunRot); // then reassign position
if (inheritAngularVelocity)
{
bulletRb.angularVelocity = angularInherited;
}
if (inheritLinearVelocity)
{
bulletRb.linearVelocity = linearInherited;
}
if (scaleMaxSpeedWithPlayer) //i cap bullet max speed, so when i move forward i increase that cap by current ship forward speed
{
bulletScript.inheridedforwardVelocity = Vector3.Dot(rb.linearVelocity, rb.transform.forward);
}
bulletRb.AddForce(bulletsList[bulletId].transform.forward * bulletLaunchForce, ForceMode.Impulse);
while writing this post, i thought for one more solution I haven’t tried it yet, it just hit me, but at this point i doubt it will work, i tried to many times for each to fail
. im thinking instead moving bullet with rb.AddForce(), move it with different method, maybe with rb.linearVelocity() since it worked when i moved spaceship with it… ??? its late night and i wont be able to write all the new code for bullet tonight, at same time i put to much time and effort writing this post just to toss it out for small hope that tomorrow changing bullet movement method will fix this bug, so i publishing this post anyway, even if tomorrow’s magic will succeed in all the fixing, maybe this post and my experience will be useful for someone else ![]()