Unity camera and player object stop at border of screen

Hello,

not sure if this is around or has been done, but trying to find out, what is the best way for a top down, to stop the camera and player when it reaches the background or border of a collision? so the camera stops, but the player is able to still move until it also reaches the border of the screen… I do see a couple tutorials online for platformers, but nothing for top down that i have found… any idea?

would I use maybe collision? or should i use rigid body 2d? any good example tutorials or sample code?

Yes, you can use collisions for this, try this tutorial it might help

https://www.youtube.com/watch?v=ailbszpt_AI

I assume its a 2D project

thank you for the tutorial, however for this tutorial, I like it, and would sorta work, however for my game, i dont want it to bound by the screen width/height, but I want the camera to move until it comes to the end of the background… see picture, hopefully this helps a little…

the weird thing is, the collision doesnt work, when the camera that has a box collider 2d and the floor box collider 2d touch, i never see a “Collision”, i put a debug.log to check and never happens, see code below and screen shot :frowning:

is Collision2d not working at all?

code below of the collider to record:

    private void OnCollisionEnter2D(Collision2D collision)
    {
        Debug.Log(collision.collider.name);
    }

so odd… I tried the collision, and never shows up, i even tried:

    private void OnCollisionEnter2D(Collision2D collision)
    {
        Debug.Log(collision.otherCollider.name);
    }

using the otherCollider… and still nothing comes up :frowning:

Maybe giving more info would help me, like maybe a pic of the hierarchy

sorry yes, So finally got it to work, which had to add RigidBody2d… but also, now that i add it, when one object collides with player object/character… it bounces around, the only way I can stop it from doing that… is on the rigid body2d, under Constraints… freeze position x, y and z… now it sorta works the way I want it… but is this the right choice of action? or is there a better way?

1 Like

Awesome, I didn’t mention it because I wanted you to figure it out. RB’s are needed for collisions to occur!
And yes, the constraints is the way to go :smile:

:stuck_out_tongue: but thank you for letting me figure it out… does feel good to finally get it slowly but surely.

1 Like