Home > Mobile >  Object Generation Gaps
Object Generation Gaps

Time:10-03

Help me understand what the problem is in Unity3D. I generate an infinite number of blocks in motion. But the higher the speed of movement, the larger the gap between objects. As far as I understand, the gap will be sampled due to the frame refresh time.

How to generate objects close to each other?

void Update()
{
    if (startspawn)
    {
        spawn1 = Instantiate(spawner);
        spawn1.GetComponent<Rigidbody>().velocity = new Vector3(15, 0, 0);
        startspawn = false;
    }
    if (spawn1.transform.position.x - startcoordinateX > size)
    {
        spawn2 = Instantiate(spawner);
        spawn2.GetComponent<Rigidbody>().velocity = new Vector3(15, 0, 0);
        spawn1 = spawn2;
    }
}

Screenshot

CodePudding user response:

Use FixedUpdate instead of Update. FixedUpdate ensures the time between calls is the same. You can read more about it here: https://docs.unity3d.com/ScriptReference/MonoBehaviour.FixedUpdate.html.

CodePudding user response:

I'll leave it here. Managed to solve the problem by simple binding to Time.deltaTime. Now, even at high speed, there are no gaps.

Just use.

spawn1.GetComponent().velocity = new Vector3(speed * Time.deltaTime, 0, 0);

CodePudding user response:

You can use Fixed update which makes the time between calls the same.

  • Related