Prevent translation overshooting

        [BurstCompile]
        struct ManageBulletsJob : IJobForEachWithEntity<Bullet, Translation, Rotation> {
            public EntityCommandBuffer.Concurrent commandBuffer;
            [ReadOnly] public float deltaTime;
            public void Execute(Entity entity, int index, ref Bullet bullet, ref Translation translation, ref Rotation rotation) {
                if (!bullet.disabled) {
                    float3 dest = bullet.destination;
                    float3 pos = translation.Value;
                    float3 targetDir = math.normalize(dest - pos);
                    targetDir.z = 0;
                    translation.Value += targetDir * bullet.moveSpeed * deltaTime;
                    float z = math.atan2(targetDir.y, targetDir.x);
                    rotation.Value = quaternion.Euler(0,0,z);
                    if (math.distancesq(pos, dest) < 0.2f) {
                        // reached target, destroy yourself
                            bullet.disabled = true;
                        commandBuffer.DestroyEntity(index, entity);
                    }
                }
            }
        }

So this code works fine when the Time.timeScale is set to 1. However, I’m now looking to implement a speedup feature into my game and I want to set the timeScale to 10. Every one of my systems works as expected except the job posted above. When I do that, not only do bullets overshoot the target, but they also switch direction when they reach the target for a single frame.

In the picture, the target is the orange square.


One frame later:

Then one frame later they finally disappear. I think they do so because they overshot their target, so they have to turn back until they hit that 0.2 distance req. I don’t want to increase that number any higher because it would just cause artifacts on other time scales and would still be frame dependent, whereas I need a frame independent solution.

In normal monobehavior code, you’d just turn on Continuous collision detection on your rigidbody, but even if there is such a way with dots physics, I’d rather avoid using physics completely. How do you handle such a case using just math? How can I tell if the translation.Value has gone past the destination? Because if I knew that, then I could just simply set its position to the destination then destroy the bullet.

Edit: Solved by following this example:

https://stackoverflow.com/questions/17692922/check-is-a-point-x-y-is-between-two-points-drawn-on-a-straight-line

You already know how much distance it fly in this frame, just check if sqr length of this vector greater than sqr length of your pos-dist. This is pretty common :slight_smile:

I had the same problem in a job. I just used math.distance and checked if it was greater than and reset it back within the limits.