Search Unity

Restricting gameobject within camera limits

Discussion in 'Scripting' started by Soumikbhat, May 21, 2015.

  1. Soumikbhat

    Soumikbhat

    Joined:
    Nov 23, 2013
    Posts:
    110
    I initially tried restricting my gameobjects movement within screen boundaries, but couldn't do much.

    What I want now is that my gameobject's movement should be restricted within the visible camera limits, i.e. even if I press the 'Right' key when the player is on the edge of my camera window - the player should not move.
    Any help would be appreciated...

    Working on a 2D game with orthographic cam...
     
  2. tranos

    tranos

    Joined:
    Feb 19, 2014
    Posts:
    180
    You could use a collider with" Trigger " enabled and "OnTriggerEnter2D" disable the input from you right key.
     
  3. Soumikbhat

    Soumikbhat

    Joined:
    Nov 23, 2013
    Posts:
    110
    But how do I detect te camera limits..x and y positions to be precise?
     
  4. WheresMommy

    WheresMommy

    Joined:
    Oct 4, 2012
    Posts:
    890
    You could go with percentage. Like, you have a screen width of 1920 e.g., this is 100%. than you can ask for the screenpoint to world position and check that Vector, this is also your 100%, so, if your player moves ahead of that Vector, bind him to back to the max position. Just a suggestion :)
     
    Soumikbhat and krougeau like this.
  5. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    456
  6. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,694
    ^ As @krougeau writes, something like this:
    Code (csharp):
    1. private Rect cameraRect;
    2. void Start()
    3. {
    4.     var bottomLeft = Camera.main.ScreenToWorldPoint(Vector3.zero);
    5.     var topRight = Camera.main.ScreenToWorldPoint(new Vector3(Camera.main.pixelWidth, Camera.main.pixelHeight));
    6.     cameraRect = new Rect(bottomLeft.x, bottomLeft.y, topRight.x - bottomLeft.x, topRight.y - bottomLeft.y);
    7. }
    Then clamp the position inside cameraRect:
    Code (csharp):
    1. Vector2 allowedPosition = cameraRect.Clamp(desiredPosition);
     
    krougeau likes this.
  7. Soumikbhat

    Soumikbhat

    Joined:
    Nov 23, 2013
    Posts:
    110
    This gives an error
    error CS1061: Type `UnityEngine.Rect' does not contain a definition for `Clamp' and no extension method `Clamp' of type `UnityEngine.Rect' could be found (are you missing a using directive or an assembly reference?)
     
  8. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,694
    The Rect.Clamp code above assumes desiredPosition is a Vector2. If it's a Vector3, or if it doesn't work in your version of Unity, just clamp it manually:
    Code (csharp):
    1. Vector2 allowedPosition = new Vector2(
    2.     Mathf.Clamp(desiredPosition.x, cameraRect.xMin, cameraRect.xMax),
    3.     Mathf.Clamp(desiredPosition.y, cameraRect.yMin, cameraRect.yMax));
     
    krougeau likes this.
  9. SheZii

    SheZii

    Joined:
    May 26, 2016
    Posts:
    4