2D Movement Within a constraint (C#)

I’m making a ShMUp game
My player is moved using transform.position and has a box collider trigger.
What are some ways I can keep the player within the constraints(In this case, in the camera’s view)?

I have tried many things - adding another box collider on the player and a box collider wall (Either killed the player or he flew straight through it)

I would Appreciate All Help!

the box colliders you tryed aint working bc you are using transform.position which just moves the player instantly ignoring all collisions if you want youre player to collide with walls then moving try using Rigidbody.velocity like MikeNewall said


GetComponet<RigidBody2D>().velocity = new Vector 2(x,y);

you can also use getaxis which i would recomend since getaxis is controller supported

Example 2

float move = Input.GetAxisRaw("Horizontal");
            movement = new Vector2(move * moveSpeed, myRigidbody2d.velocity.y);
            myRigidbody2d.velocity = movement;

heres some documentation http://docs.unity3d.com/ScriptReference/Input.GetAxis.html

If you want to stick with Transform.position = …
Then you Can use worldToScreenPoint(newPosition) and check if the screen point is still in view, if its outside then readjust.