Instantiated object's position with dynamic rigidbody resets coordinate in first step

Here is what I want:

  • Spawn a bullet, set position, rotation and velocity
  • Lock bullet to x-z plane and lock rotation to y-axis rotation

Here is what I do:

  • Cloning a deactivated projectile from hierarchy, setting position / velocity / rotation and activating it
  • Adding a script to lock the position and rotation
  • The lock script reads the entity position, resets the y value and sets it on the entity again ans synchronizes the body’s position via calling syncEntityToBody()

Here is what is odd when NOT adding the position lock script:

  • The bullet appears for a few frames at 0,0,0 before being shown at the actual position, moving the right direction with the right orientation

Here is what happens when adding the position lock script:

  • When the entity position is read in the initialize call, it has the correct coordinate from spawning the entity
  • When the first frames are evaluated, getting the position via getPosition gives me the 0,0,0 coordinate (contrary to the initialize call)

I have tried using teleport, ignoring the first few frames and changing the activation order. It doesn’t help. It seems that a freshly initialized rigidbody starts out an initializer position before being updated. It reminds me of what happens in Unity when doing things in the Update call when you’re supposed to use the FixedUpdate callback to be in sync with the physic stepping.

Any suggestions how to fix this? It’s also not nice that I can see the object at 0,0 when the thing spawns. I read that the fixed update calls have been removed in the past; how am I able now to do things synchronized with the physics stepping?

Hi @Eike_Decker and welcome!

I have noticed this issue in my projects as well. My workaround was to make sure there is nothing at 0,0,0 in my scene so it wouldn’t effect anything. (However, in new projects I haven’t noticed the problem anymore, but maybe I’m using different logic now).

Please feel free to open a GitHub issue.

Maybe you can use postUpdate for this.

https://developer.playcanvas.com/en/user-manual/scripting/application-lifecyle/

Do your bullets have a dynamic rigidbody? Then maybe that’s why I don’t notice the problem anymore, because currently I use trigger entities as a bullet. Bullets with a rigidbody are effecting other dynamic rigidbodies at impact and that’s not what I want.

Yes, my bullets use a dynamic rigidbody. I assume the body position in the physics simulation is updated with a delay. I found a workaround:

  • Moving the prefab object to a far distant location - it spawns actually there
  • Ignore positions in the update loop when being at the initial position

It’s acceptable for me for now, but it would be better if this initialization step would just work.