Gameobject to stay within the view of the camera - c#

I’m trying to make a top down survival game but I need to make the play stay within the view of the camera, and say if he got to the edge he would stop.

Thanks in advance.

What you could do is get the coordinates for the edge of the screen, so move the player to the edge and get his X position or whatever you’re using and put something like this

//7 = edge of the screen
if(transform.position.x >= 7)
  transform.position.x = 7;
else if(transform.position.x <= -7)
  transform.position.x = -7;

You should still be able to move the player between these two values. I can’t imagine what your game is going to look like so just give this a shot.

This live training session from Unity will help you with your current problem and maybe more regarding Top Down Shmup.

Use the Camera frustum its explained here: