How to lock the players position by Z?

I want him to be able to move by X and Y but not by Z! I’m interesting in locking the players position

Is there any more elegant solution than something like this? :

void FixedUpdate()

{
   transform.pozition.z = 0.0f;
}

I don’t know any other solutions. I am always using this. Maybe you can use a Rigidbody. Rigidbodies have the possibility to lock one or more axis.

I am always making a Script that sets the Z-Position of the Player in a FixedUpdate to the Z position it started with

If it has a Rigidbody attached,go to Rigid body>Constraints. Then check the box of Freeze-Position Z & also Freeze-Rotation Z…That will make its Z-position freeze…Hope this helps you