Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

(Scaling 2d objects)Very confused by something very simple

Discussion in '2D' started by Thrazamund, Apr 14, 2017.

  1. Thrazamund

    Thrazamund

    Joined:
    Apr 14, 2017
    Posts:
    36
    Hello friends, lvl 1 Unity noob here. I was hoping to get some help. I've been messing around in Unity and I've come upon a strange issue with scaling which I can't figure out or find information for. So, I created a background sprite in gimp and I drew it at 4096x4096 thinking it's always better to draw too big then scale down. I then brought it into my assets and set the sprite settings down to 2048x2048. I pulled it into my scene and it was huge(my camera is at 1280x720) but I figured that's no problem I will just scale it down using the Transform settings. I did until I got the size I liked and I thought that was it.

    The issue came about when I added a boxcollider2d to it. It turns out that even though it's scaled down the boxcollider's size says it's way bigger than the world space it's taking up. The boxcollider fits the background perfectly and even if I change the gameobject's scale in the Transform to 100 or down to 0.5 the boxcollider always visually scales with it but if I look at the size of the boxcollider in the settings it's always corresponding to this original size of the image and does not change. I do not understand this behavior at all. This is causing an issue with my code where I was finding the width of the background's boxcollider so I could continue placing more backgrounds behind it, like an endless runner. Any help is greatly appreciated. Thanks!
     
  2. Zephus

    Zephus

    Joined:
    May 25, 2015
    Posts:
    356
    You can just edit the BoxCollider2D. Not sure why you think you're stuck with the automatically generated one. There's an edit-button in the component.
    Also play around with the pixels per unit in the import settings, but be consistent with that and the size of your sprites. If you don't need a gigantic texture, then don't use a gigantic texture. It's pretty much a waste of space and memory. Just create everything with the right scale from the start.
     
    theANMATOR2b likes this.
  3. Thrazamund

    Thrazamund

    Joined:
    Apr 14, 2017
    Posts:
    36
    I appreciate your clear response. My issue is that if I edit the boxcollider to be smaller it no longer fits the sprite. They seem to be giving conflicting measurements of size. Let me try to explain what I'm doing and hopefully my issue will make more sense. My goal is to make an endlessly scrolling background. I am taking a simple background sprite with a boxcollider, duplicating it a few times and putting them in a row horizontally. I then have my main camera set so it will slowly scroll horizontally along these backgrounds. On my main camera is an invisible child object with a collider/rigidbody that follows the camera behind the player's viewable screen. With my code I take all of my backgrounds put them in an array, I find the background that has the highest x value and set it as the last background. Whenever my camera's invisible child object collides with a background(since it's behind the camera this will happen offscreen) it then takes that background and places it after the background with the highest x value. Theoretically this should create an endlessly scrolling background. Here is my code:
    Code (csharp):
    1.  
    2. public class BGSpawner : MonoBehaviour {
    3.  
    4.     [SerializeField]
    5.     private GameObject[] backgrounds;
    6.  
    7.     float lastBackgroundX;
    8.  
    9.  
    10.     // Use this for initialization
    11.     void Awake () {
    12.  
    13.         //backgrounds = GameObject.FindGameObjectsWithTag("Background");
    14.  
    15.         lastBackgroundX = backgrounds[0].transform.position.x;
    16.  
    17.         for (int i = 1; i < backgrounds.Length; i++)
    18.         {
    19.             if (lastBackgroundX < backgrounds.transform.position.x)
    20.             {
    21.                 lastBackgroundX = backgrounds.transform.position.x;
    22.             }
    23.         }
    24.     }
    25.  
    26.     private void OnTriggerEnter2D(Collider2D target)
    27.     {
    28.         if (target.tag == "Background")
    29.         {
    30.             Vector3 temp = target.transform.position;
    31.  
    32.             float width = ((BoxCollider2D)target).size.x;
    33.  
    34.             //Debug.Log(width);
    35.  
    36.             temp.x = lastBackgroundX + width;
    37.  
    38.             target.transform.position = temp;
    39.  
    40.             lastBackgroundX = temp.x;
    41.         }
    42.     }
    43. }
    44.  
    and this code seems to work. It does place the backgrounds that the camera has passed and places them after the background with the highest x value. The issue is that because for some reason the boxcollider kept the values of the original sprite size and does not use a size that accurately reflect its world space value. So when I use this line
    float width = ((BoxCollider2D)target).size.x;
    to get the right width to place my next background the background gets placed at a distance that would reflect the original sprite width and not its current width. That is my confusion
     
    Last edited: Apr 18, 2017
  4. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    please update your post using Code Tags to make it easy for people to read your code.
     
  5. Thrazamund

    Thrazamund

    Joined:
    Apr 14, 2017
    Posts:
    36
    The first picture is of my scene. The boxcollider is saying it's size is 40.96x40.96 this is my sprite's original value but I since resized it to be much smaller. You can see under the transform I set scale to 0.3 yet the boxcollider thinks its still at scale 1. I'm sure the boxcollider's value is wrong in world space because my camera is set to 0, 0, 0 and look at the transform placement of the background in the picture 32.78 x value. If it's size in world space was 40.96x40.96 its x value would be a lot higher so something is inaccurate or I'm fundamentally misunderstanding how this works.

    The second picture shows my code works but when it gets float width = ((BoxCollider2D)target).size.x; once again it's getting this value that's wrong. As if my sprite/collider were at scale 1 when the transform is set to scale 0.3. That is why it's placing the background too far away. If I resize my boxcollider it no longer fits the bounds of the sprite though.

    My third picture illustrates the issue most clearly. In this picture I've clearly set my backgrounds scale to its original size(much bigger) but notice my boxcollider has kept the exact same size value of 40.96x40.96. I could set the scale to 2000 and my boxcollider would still have a size value of 40.96x40.96. That is the root of my confusion. Any help is greatly appreciated!
     

    Attached Files:

    Last edited: Apr 18, 2017
  6. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    Collider size is a value in local space, and won't change with scale. You can multiply the size by the scale, or perhaps use the SpriteRenderer's bounds instead of the collider's size.
     
  7. Thrazamund

    Thrazamund

    Joined:
    Apr 14, 2017
    Posts:
    36
    Thanks for the reply. I multiplied the collider width by the scale in the code and that worked :) As far as the collider size value being in local space I'm having a little trouble picturing how that works. I believe local space means everything about the object/property is referring to a parent object/property whereas world space would be just as is placed in the world. Do you mean that the collider is child to the spriterenderer? Its size is basically its own version of scale? 40.96x40.96 being the same as 1x1 and so when you change the object's scale the value of the boxcollider is going to remain 1x1 even though it grows and shrinks?
     
  8. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    Yeah compare it to local position. If you set a child object to local position (1,0), and then scale up the parent, the local position doesn't change. This is because when you scale an object, you're scaling its whole coordinate system. Child objects still use this coordinate system. So local position (1, 0) in a coordinate system scaled up by 100 would be world space (100, 0) if the scaled object is at the world origin (0,0). Child objects consider the parent object as the origin for local values.

    Local in effect means "relative to the parent's coordinate system" where that system has already been offset, rotated, and scaled by the parent transform.
     
    Last edited: Apr 24, 2017
  9. Thrazamund

    Thrazamund

    Joined:
    Apr 14, 2017
    Posts:
    36
    Okay, that clarifies things. Thanks for taking the time to respond! I will put this knowledge to use :)
     
    LiterallyJeff likes this.