What does single pipe means in c#?

The solution in the lecutre by freezing and unfreezing create another bug that is when we unfreez the rotation, it unfreez all the 3 axis when in fact we want to unfreez only the z axis.
After some research I came up with this solution:

void ApplyRotation(Vector3 rotation)
    {
        rb.freezeRotation = true; 
        transform.Rotate(rotation * rotateThrustVelocity * Time.deltaTime);
        rb.freezeRotation = false;
        rb.constraints =
         RigidbodyConstraints.FreezeRotationX |
          RigidbodyConstraints.FreezeRotationY |
          RigidbodyConstraints.FreezePositionZ;
    }

so I just added the last statement which uses the bitwise to combine what we want to freez.
I still don’t understand how does this single pipe works or why it works, if anyone can explain, I would really appreciate it

1 Like

Hi @Yasser_Altamimi. Welcome to the community

This is a bitwise ‘or’. In this case, it is ‘setting’ the bits to create the required value.

In Unity, FreezeRotationX = 16, FreezeRotationY = 32 and FreezeRotationZ = 64.
In binary, these are 0001 0000, 0010 0000 and 0100 0000 respectively.

or-ing the values is like adding them together
FreezeRotationX | FreezeRotationY is like

    0001 0000    (16)
 or 0010 0000    (32)
-------------
    0011 0000    (48)

which represents both FreezeRotationX and FreezeRotationY

1 Like

Very clear, Thanks a lot!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms