Search Unity

Best way to stop the camera from at edge of level

Discussion in 'Scripting' started by JayCally, Nov 13, 2009.

  1. JayCally

    JayCally

    Joined:
    Apr 22, 2009
    Posts:
    45
    I have a game level that is just a plane with the camera above the player looking down. The player can go to the edge of the level but I don't want the camera seeing past the edge. If you have every played miniGORE for the iPhone, I'm trying to set the camera up like that.

    What is the best way of doing this?

    The attached photo shows the camera not moving past a certain point and the character not moving past a "box collider".
     

    Attached Files:

  2. raiden

    raiden

    Joined:
    Feb 8, 2009
    Posts:
    333
    Just find the minimum and maximum positions on both sides and clamp the transform to keep from going beyond those positions.

    Code (csharp):
    1.  
    2. var minPosition : float = -10.0; // left border
    3. var maxPosition : float = 10.0; //  right border
    4.  
    5. function Update() {
    6.     transform.position.x = Mathf.Clamp(transform.position.x, minPosition, maxPoosition);
    7. }
    Something like that.

    -Raiden
     
  3. JayCally

    JayCally

    Joined:
    Apr 22, 2009
    Posts:
    45
    Thanks for the reply. Is the min/max positions taken from the size of the level? And this script gets added to the camera? :oops:
     
  4. raiden

    raiden

    Joined:
    Feb 8, 2009
    Posts:
    333
    The min and max positions would be the borders of your level, for example in my Breakout clone game, I figured out the borders by moving the paddle as far as I want to go to the left:



    You can see in the Transform "x" position I know I'm going to clamp my paddle from moving no further to the left @ -12, which will be the minimum amount in the clamp, then you just move your object all the way to the right you want to go, and that will be the maximum value in the clamp. Just use the positions in the Transform and you will be able to find your limits. Oh, and yes you would use this script on your camera.

    -Raiden
     
  5. JayCally

    JayCally

    Joined:
    Apr 22, 2009
    Posts:
    45
    Raiden, thanks so much.
     
  6. abdziabdziuba

    abdziabdziuba

    Joined:
    Feb 8, 2021
    Posts:
    2

    Yeah, so i used this code. but:
    CS1612 C# Cannot modify the return value of 'Transform.position' because it is not a variable