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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

2D Object Movement

Discussion in '2D' started by shawnrevels, Apr 12, 2016.

  1. shawnrevels

    shawnrevels

    Joined:
    Aug 13, 2014
    Posts:
    86
    So i know its pretty basic scripting.. But does anyone have an easy way to have an object, say a square, move left and right without leaving the screen. The camera movement would be set on the same axis at all times. I know i can make a level bounds and constrain the box to these bounds but i figured there's an easier way. Now of course keep in mind that the screen res will change based on what phone is being use, since this will be mobile, so using something like x = Random.Range(-4.00f , 4.00f); or something like that wouldn't really work. Or at least i don't think. Any thoughts?
     
  2. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,802
    To keep an object on screen you have to know the dimensions of the object, and the dimensions of the screen. With that info you can check if the object's edges have passed the boundaries of the screen.

    You can get the half-dimensions of the screen in world-space like this:
    Code (CSharp):
    1. Vector2 screenHalfSize = new Vector2((Screen.Width / PixelsPerUnit) * 0.5f, (Screen.Height / PixelsPerUnit) * 0.5f);
    replace PixelsPerUnit with the pixels per unit you used for your sprite. Imported sprites default to 100.

    Since "Screen.Width" and "Screen.Height" return a value in pixels, you must either convert your screen values into unity units (world space), or convert your object's values into screenspace units (pixels) in order to compare them.

    Dividing by PixelsPerUnit converts the pixel units to unity units.

    Then get the half-dimensions of your object like this:
    Code (CSharp):
    1. Vector2 objectHalfSize = objectCollider.bounds.size * 0.5f;
    Here I'm using a collider because it's easy. You can also get the Sprite bounds and size if you don't want a collider.

    Then to keep your object on-screen you have to do these checks on the object every frame:
    Code (CSharp):
    1. private void LateUpdate()
    2. {
    3.     float objectLeftEdge   = transform.position.x - objectHalfSize.x;
    4.     float objectRightEdge  = transform.position.x + objectHalfSize.x;
    5.     float objectTopEdge    = transform.position.y + objectHalfSize.y;
    6.     float objectBottomEdge = transform.position.y - objectHalfSize.y;
    7.  
    8.     Vector3 newPosition = transform.position;
    9.  
    10.     if(objectLeftEdge < -screenHalfSize.x)
    11.     {
    12.         newPosition.x = -screenHalfSize.x + objectHalfSize.x;
    13.     }
    14.     else if(objectRightEdge > screenHalfSize.x)
    15.     {
    16.         newPosition.x = screenHalfSize.x - objectHalfSize.x;
    17.     }
    18.  
    19.     if(objectBottomEdge < -screenHalfSize.y)
    20.     {
    21.         newPosition.y = -screenHalfSize.y + objectHalfSize.y;
    22.     }
    23.     else if(objectTopEdge > screenHalfSize.y)
    24.     {
    25.         newPosition.y = screenHalfSize.y - objectHalfSize.y;
    26.     }
    27.  
    28.     transform.position = newPosition;
    29. }
     
    Last edited: Apr 13, 2016
  3. shawnrevels

    shawnrevels

    Joined:
    Aug 13, 2014
    Posts:
    86
    I got what your saying. Thanks buddy.
     
    LiterallyJeff likes this.