How to make player not go past camera's edges

Im making a space invaders remake and I want to make sure the player stays in the camera’s bounds. Other answers on unity answers don’t really tell me how to do it properly. please help

You can use Mathf.Clamp to bound your movement to the left or right just within an amount of space and cannot surpass it.

Here is the code you may want to take as reference:

	position.x = Mathf.Clamp (position.x, -2.2f, 2.2f)

Written in C#

This line bounds the character X position from -2.2 to 2.2 in world space in the editor window. You can change these variables to suit your purpose and remember to put “f” behind the numbers 58575-may-tinh.png

Put this into the script that used to control the spaceship and into the Update function of it