Instantiate gameobjects have differents speed

Hello,

I instantiate random numbers of enemies and when all appears, they translate down straight properly but with different speed.

I tried with Time.deltaTime, Time.maximumDeltaTime, Time.time, and even Time value. Nothing happends, all the time, my gameobjects goot always different speeds.

Here my C# code :

int enemyIndex = 0;

float distance;
float speed = 0.005f;
float padding = 0.5f;

private EnemySpawn ES;

private Vector3 borderMin;

private float time;

// Use this for initialization
void Start ()
{
    ES = GameObject.Find("Enemy_Spawner").GetComponent<EnemySpawn>();

    distance = transform.position.z - Camera.main.transform.position.z;
    borderMin = Camera.main.ViewportToWorldPoint(new Vector3(0, 0, distance));
}

// Update is called once per frame
void Update ()
{
    if (enemyIndex < ES.getEnemyWave().Length)
    {
        if (ES.getEnemyWave()[enemyIndex].activeSelf)
        {
            enemyMoves(ES.getEnemyWave()[enemyIndex]);
            if (enemyOutOfBounds(ES.getEnemyWave()[enemyIndex]))
            {
                Destroy(ES.getEnemyWave()[enemyIndex]);
                enemyIndex++;
            }
        }
    }
}

void enemyMoves(GameObject gb)
{
    gb.transform.Translate(Vector3.down * speed * Time.time);
}

public int getEnemyIndex()
{
    return enemyIndex;
}

bool enemyOutOfBounds(GameObject gb)
{
    if (gb.transform.position.y < borderMin.y - padding)
    {
        return true;
    }

    else
    {
        return false;
    }
}
  1. Try change speed to bigger value, 0.005f; is very small per second if you use metric scale.
  2. gb.transform.Translate(Vector3.down * speed * Time.deltaTime); change to gb.transform.position += Vector3.down * speed * Time.time;

And check if you don’t move object and his parent…
Maybe it help you :smiley:

Hello @plangiewicz,
Thanks for your help.
So I tried gb.transform.position += Vector3.down * speed * Time.Time and the issue stays. But I tried with gb.transform.position += Vector3.down * speed * Time.maximumDeltaTime and the enemies are moving at the same speed now.

However when I played two times the editor, one time the enemies crossed the screen in 2 seconds, and the “next play”, they crossed the screen in 6 seconds. Maybe due to the Time.maxiumDeltaTime ?

About the parent, it’s an empty GameObject with the script and here the function to create enemies :

void setenemyPositionAndCharac()
    {
        for (int i = 0; i < enemies_wave.Length; i++)
        {
            float temp = Random.value;
            temp = Mathf.RoundToInt(temp);
            if (temp == 0)
            {
                enemySpawn = new Vector3((Random.value * borderMin.x) + padding, transform.position.y, distance);
            }
            else if (temp == 1)
            {
                enemySpawn = new Vector3((Random.value * borderMax.x) - padding, transform.position.y, distance);
            }

            enemies_wave *= Instantiate(enemyPrefab, enemySpawn, Quaternion.identity);*

enemies_wave*.tag = “Enemy”;*

EC = enemies_wave*.GetComponent();
_EC.setEnemyID(i);*_

EC*.setSprite(enemySprite[setEnemyColor()]);*
}
}