I need Help Loop Move

Hello! :slight_smile:
I would like some help. Please
i want all objects to be in a line.
All Objects move Forward With some Speed
and reset position when object is on Z position > 10

but have a gap with each other
When i speed up objects
how to do it without this gap between them
thank you !!

public float Move_Speed = 1.0f;
public float Reset_Pos = 10f;

void Update()
{
    transform.position += transform.forward * Move_Speed * Time.deltaTime;
    if (transform.position.z >Reset_Pos)
    {
        transform.position = new Vector3(0,0,0);
    }
}
  • Video View
  • Image View

1 Answer

1

I can’t be certain, but I think the problem may be that some error is introduced when you reset the position to 0,0,0. You are discarding some of the movement which disrupts the relative position between blocks.

When a block moves beyond Reset_Pos, you need to consider how far past Reset_Pos it traveled when resetting its position.

Perhaps it will be clearer to show in code:

void Update()
{
    transform.position += transform.forward * Move_Speed * Time.deltaTime;
    if (transform.position.z >Reset_Pos)
    {
        float newZ = transform.position.z - Reset_Pos
        transform.position = new Vector3(0,0,newZ);
    }
}

This is the equivalent of performing the modulus operation on the position.

bro i had tried many things for many months and I didn't expect this to be the solution my mind explode. thank you very much

–