'RaycastHit returns no hit when objects swap positions

I am moving a row of boxes and want to create an infinite effect. Every last box will swap its position with the cloned block in the beginning (see image).

For Debug purposes I am using a boxcast as raycast for each box. It does not move like the boxes but stays like the first row (1.) in the picture.

The boxcast works fine until I change the 5th box position with the clone box and deactivate the clone box. My first boxcast hits no collider for 6-10 frames after positions have been swapped even though box 5 transform.position is exactly on the boxcast position.

I have tried updating Physics Transform before Boxcasting. Tried different Raycasts(Overlapbox, Ray,...).

scenario

    private void MoveRowOfBlocks(float m)
    {
//debug variable set true when setting box position to clone position
        bool exceed = false;
        for (int i = 0; i < movingBlockRow.Length; i++)
        {
            GameBlock go = movingBlockRow[i].GetComponent<GameBlock>();
            // Block exceeds valid area -> Swap with clone block
            while (movingDir == Vector2.right && go.transformPositionCache.x + m > GameField.xFieldExtent_M - GameField.blockWidth / 2
               || movingDir == Vector2.left && go.transformPositionCache.x + m < -GameField.xFieldExtent_M + GameField.blockWidth / 2
               || movingDir == Vector2.up && go.transformPositionCache.y + m > GameField.yFieldExtent_M - GameField.blockWidth / 2
               || movingDir == Vector2.down && go.transformPositionCache.y + m < -GameField.yFieldExtent_M + GameField.blockWidth / 2)
            {
                exceed = true;
                go.transformPositionCache -= GameField.totalWidth * movingDir;
                //go.transform.position = go.transformPositionCache + (m * new Vector2(Mathf.Abs(movingDir.x), Mathf.Abs(movingDir.y)));
                blockExceedCount += movingSign;
                Debug.Log(go.name + " Exceeds field. Position from beginning. " + "New Position: " + go.transformPositionCache 
+ " movingDir: " + movingDir + " Frame: " + debugFrameCount);
                if (blockClone != null || clonedBlock != null)
                {
                    blockClone.SetActive(false);
                    blockClone = null;
                    clonedBlock = null;
                }
                cachedExceededBlock = go;
                debugCount++;
            }
            debugCount = 0;
            go.transform.position = go.transformPositionCache + (m * new Vector2(Mathf.Abs(movingDir.x),Mathf.Abs(movingDir.y))); 
            if (exceed) Debug.LogError(go.transform.position);
        }
        

        // Move Cloned Object
        if (blockClone != null)
        {
            Vector2 dest = (Vector2)clonedBlock.transform.position + (GameField.totalWidth * cloneMovingDir) * -1;
            blockClone.transform.position = dest;
        }
        if (exceed)
        {
            Physics.SyncTransforms();
            Debug.LogError(movingBlockRow[4].transform.position);
        }
// Using Boxcasts foreach init box position for debugging
        foreach (Vector3 v in gameField.gridPointsWorldPosition) // gridpointsworldposition is initial box position (1.)
        {
            RaycastHit2D rayHit = Physics2D.BoxCast(v, new Vector2(GameField.blockWidth/2, GameField.blockWidth/2), 0, Vector2.zero);
            if (rayHit.collider == null)
            {
                Debug.LogError("NO BOX FOUND IN CELL " + v + " Box position: " + movingBlockRow[4].GetComponent<BoxCollider2D>().transform.position 
                     + " Frame: " + debugFrameCount); // Rayhit.collider is null even though box position = v
            }
        }
        debugFrameCount++;
    }



Solution 1:[1]

If you want the object to act as if it doesnt have a Rigidbody, set the Rigidbody to static.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Nimantha