One child object move differently as 0.0001f when moving parent

Hello. I’m on developing game in 2D.


A parent gameobject has 3 children object.

  • child 1’s localposition : (-25.6, 0, 0)
  • child 2’s localposition : (0,0,0)
  • child 3’s localposition : (25.6, 0, 0)

Children have SpriteRenderer and nothing else.

(all objects are not rigidbody, and don’t have any colliders.)

I want to move 3 children object in same speed, so I translate parent object’s transform at LateUpdate.

(Using LateUpdate because the parent object must follow camera move and camera should follow other game object (e.g. Player) ).

The move is only horizontal. (Only X move.)


[Here’s problem]

  • Occasionally one of the children moves 0.0001f point more or less on a frame. (randomly)
  • And the wrongly moved child get back accurate position at next frame
  • So, while playing, black line (background color) appears between the children sprites and disappears soon (disappears at next frame).


My camear is Orthograpic and size 7.2.

The Sprite of children are same size of 2560x2560 pixel. (Pixels per unit is 100)

this is LateUpdate code, and it’s added to parent gameobject.


public void MoveParallaxing(){

        parallax = Mathf.Floor((cam.position.x - previousCamsPos.x) * parallaxScale * 10000) / 10000;

        if (Mathf.Abs(parallax) > 0.01f){
            
            float backgroundTargetPosX = Mathf.Max(transform.position.x + parallax, minX);
            Vector3 backgroundTargetPos = new Vector3(backgroundTargetPosX, transform.position.y, transform.position.z);
            transform.position = backgroundTargetPos;

        }
        previousCamsPos = cam.position;
    }

What am i missing? Please give me some clue.
Thank you in advance.


That’s just normal floating point precision. Keep in mind that the actual position of your sprites are defined in local space, so they don’t move at all. The worldspace position of an object that has a parent is always calculated based on the parents position, rotation, scale and the object’s local position. The greater a floating point value gets before the binary point / decimal point, the less precision you have behind the binary / decimal point. I’ve posted a table over here. So if your parent is at (0,0,0) the worldspace position of your objects are “± 25.6” (25 requires 5 bits before the binary point). Though if you have the parent to “300” the worldspace position would be 325.6 which requires 9 bits before the binary point. So you have 3 bits less behind the binary point. Finally keep in mind that numbers are stored in binary not decimal. Not every decimal number can be precisely represented. Just like you can’t represent (1 / 3) in decimal precisely you can’t represent different numbers precisely in binary.

I recommend to watch this computerphile video about floating point numbers