How to freeze rotation on the Z axis with a 2d Rigidbody?

I would like to write code that will freeze my rotation in the Z axis. I don’t know whether I should use Rigidbody constraints, freezeRotation, or a Quaternion/Euler. I am confused on how I should go about writing the code to achieve this in C#?

I think the easiest way is the following:

void Update(){
 Vector3 currentRotation = transform.localEulerangles;
 currenRotation.z = 0;

 transform.localEulerangles = currentRotation;

Well if you don’t want the Z-Axis to be locked to 0 you change the value.

Hope it helps