Peculiar problem with frame-based bullets generator
I have very interesting problem with frame-based bullet generating and changing speed.
Imagine schmup type game. Bullets are shot from right to left horizontaly in line. After completing one round/wave speed is raised by 10%. Until speed is doubled. Then keep that double speed.
Problem is that gap between bullets in one round is slightly different from gap size in other round with different speed. I think that this is happening because of Mathf.RoundToInt(framesBetweenBullets).
1 sec = 30 frames TimeBetweenBullets = 1f(sec) SpeedChanger = 1f - 2f
30 * (TimeBetweenBullets / SpeedChanger); this number is not round, but i need Int for frame based, so after Round.Int i lose precision. but even if i double speed to 2 and frames between shots are at half (15) (all Ints) the gap is larger and i don't know why.
Few thoughts: it seems like nonsense to cut frames between shots by 10% from 30 to 15 frames to achieve double speed and multiply bullet speed from 1 to 2 with same variable.
i don't need that bullet speed is raised by precise 10%. so i have tried to store ratio from rounded variable framesBetweenBullets. and use that to modify bullet speed with no success.
i need some magic:) number that will multiply bullet speed (and many other things in my game) to correct speed with dependecy on rounded framesBetweenBullets number, i think.
Can someone from ADVANCED programmers help me?
And please don't write anything with Time.DeltaTime, which i think is completely useless in most part of any game.
Sorry for my bad english. Small clean example project in attachment (U2017.1) Switch to android project and disable V Sync Count in QualitySettings to allow 30 FPS.
code example Bullet.cs: void Awake(){ targetFPSrate = (1f / targetFPS); }
public void UpdateMove(){
m_transform.position += m_transform.up * ((m_speed * speedChanger) * targetFPSrate);
}
it seems that when i multiply m_transform.up with some number 30 times per second, object is moving differently than with multiply m_transform.up with double the some number 15 times per second. it should be moving the same distance, i think.
code example ShotBase.cs:
public void Update() {
framesCounter++;
if (framesCounter > framesBetweenShotsRounded)
{
ShotBullet(m_Bullet, m_BulletSpeed, m_SpeedChanger);
}
}
public void Shot() {
framesBetweenShotsNotRounded = targetFPS * (m_BulletBetweenTime / m_SpeedChanger);
framesBetweenShotsRounded = Mathf.RoundToInt(framesBetweenShotsNotRounded);
framesCounter = framesBetweenShotsRounded;
}