Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Clamp fully within screen size

Discussion in 'Scripting' started by pKallv, Jun 15, 2014.

  1. pKallv

    pKallv

    Joined:
    Mar 2, 2014
    Posts:
    1,122
    I am developing a 2D game and want to clamp my object within the screen size. I am using the following code to do so:

    Code (csharp):
    1.  
    2. //Check & correct if object is outside of screen boundaries
    3. Vector3viewPos = Camera.main.WorldToViewportPoint(selected_GameObject.transform.position);
    4. viewPos.x = Mathf.Clamp01(viewPos.x);
    5. viewPos.y = Mathf.Clamp01(viewPos.y);
    6. selected_GameObject.transform.position = Camera.main.ViewportToWorldPoint(viewPos);
    7.  
    This works except that half of the game objects is outside of the screen boundary and I do not want any of the game object to get outside of the screen..

    I then tested with initiating the size of the game object with the following code to get 50% of the objects size:

    Code (csharp):
    1.  
    2. objWidth = selected_GameObject.renderer.bounds.extents.x;
    3. objHeight = selected_GameObject.renderer.bounds.extents.y;
    4.  
    I am testing with the cardWidth & cardHeight to try to impact the clamp in various ways but do not get it to work properly. Looking at it it looks pretty simple.

    Would really appreciate if someone can help me out with this piece of code to make sure that 100% of the object stay on the screen at all times.
     
  2. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    use a Mathf.Clamp(viewPos.x, objWidth/2, Screen.width - objWidth/2); ?
     
  3. pKallv

    pKallv

    Joined:
    Mar 2, 2014
    Posts:
    1,122
    I am afraid it does not work, causes no boundaries at all.
     
  4. pKallv

    pKallv

    Joined:
    Mar 2, 2014
    Posts:
    1,122
    I did not get the Clamp to work properly but solved it with this code:

    Code (csharp):
    1.  
    2. //Check & correct if object is outside of screen boundaries
    3. floatfinalWidth = maxWidth - cardWidth;
    4. floatfinalHeight = maxHeight - cardHeight;
    5.  
    6. if (selected_GameObject.transform.position.x <= -finalWidth) {
    7. selected_GameObject.transform.position = newVector3(-finalWidth, selected_GameObject.transform.position.y, selected_GameObject.transform.position.z);
    8. }
    9. elseif (selected_GameObject.transform.position.x >= finalWidth) {
    10. selected_GameObject.transform.position = newVector3(finalWidth, selected_GameObject.transform.position.y, selected_GameObject.transform.position.z);
    11. }
    12.  
    13. if (selected_GameObject.transform.position.y <= -finalHeight) {
    14. selected_GameObject.transform.position = newVector3(selected_GameObject.transform.position.x, -finalHeight, selected_GameObject.transform.position.z);
    15. }
    16. elseif (selected_GameObject.transform.position.y >= finalHeight) {
    17. selected_GameObject.transform.position = newVector3(selected_GameObject.transform.position.x, finalHeight, selected_GameObject.transform.position.z);
    18. }
    19.  
     
  5. superpig

    superpig

    Drink more water! Unity Technologies

    Joined:
    Jan 16, 2011
    Posts:
    4,613
    That code does functionally the same thing as jister's solution, fwiw, so I'm guessing that when you tried his one out then you didn't implement it quite correctly. I'd write your code more succinctly as:

    Code (csharp):
    1.  
    2. Vector3 finalPosition = selected_GameObject.transform.position;
    3. finalPosition.x = Mathf.Clamp(finalPosition.x, -finalWidth, finalWidth);
    4. finalPosition.y = Mathf.Clamp(finalPosition.y, -finalHeight, finalHeight);
    5. selected_GameObject.transform.position = finalPosition;
    6.  
     
    glenneroo likes this.
  6. pKallv

    pKallv

    Joined:
    Mar 2, 2014
    Posts:
    1,122
    Now that was very nice of you and it finally worked, BIG thank you @superpig :) ...and yes I did not implement it correctly and now i learned how to use it. This is a significantly more elegant solution.