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

Scaling sprite with BoxCollider2D attached

Discussion in '2D' started by Limeoats, Aug 22, 2014.

  1. Limeoats

    Limeoats

    Joined:
    Aug 6, 2014
    Posts:
    104
    Hello.

    I'm having an issue when trying to scale a sprite that has a BoxCollider2D attached.
    I have tried both adding the collider directly to the sprite, and I have also tried creating a prefab and adding the collider to that instead.
    The issue is that when I scale the sprite (Press 'r' in the scene view when it is selected and then scale up or down), it appears that the sprite scaled properly and the box collider also appears to scale with it.
    However, when I press Play and the game starts, only the collider scaled. The sprite is still the original size even though in the scene view, the sprite is the scaled size.

    Does anybody know why this might be happening?
     
  2. herman111

    herman111

    Joined:
    May 11, 2014
    Posts:
    119
    if its animated the collider won't scale...I used the animation window to scale and move the colliders with the animation
     
  3. Limeoats

    Limeoats

    Joined:
    Aug 6, 2014
    Posts:
    104
    Currently it is not animated at all. It is simply a prefab with a BoxCollider2D, a Rigidbody2D, and a few scripts.
     
  4. Vitor_r

    Vitor_r

    Joined:
    May 23, 2013
    Posts:
    93
    What are these scripts doing?
     
  5. Limeoats

    Limeoats

    Joined:
    Aug 6, 2014
    Posts:
    104
    The sprite I am trying to scale is an enemy.

    It contains a character controller (which references the BoxCollider2D for collision purposes), and it has an AI script which deals with when to shoot projectiles at the player.
    Those are the only scripts attached to the sprite.

    I had a feeling that it had something to do with the character controller since my player also seems to have the same problem if I try to scale him.

    The part that is confusing me is that I have prefabs for other objects (my ground, for example), which have box colliders attached to them. If I scale those prefabs, the box collider scales appropriately as well and works exactly as expected.
    I am only having trouble with scaling the sprites that have character controllers on them.
     
  6. Vitor_r

    Vitor_r

    Joined:
    May 23, 2013
    Posts:
    93
    Without seeing the sprites i can't help much more.
    But you can try disable one of these scripts and see if the problem persist. If the sprites maintan the scale with the script disable, something in the script is changing it.
     
  7. Limeoats

    Limeoats

    Joined:
    Aug 6, 2014
    Posts:
    104
    That's the troubling part.
    If I disable the BoxCollider2D on the sprite but keep my character controller script on, it still collides with other objects even though it has BoxCollider2D in the Inspector window.
     
  8. Vitor_r

    Vitor_r

    Joined:
    May 23, 2013
    Posts:
    93
    If you disable the character controller script, the sprite and the BoxCollider2D maintain the scale?
     
  9. Limeoats

    Limeoats

    Joined:
    Aug 6, 2014
    Posts:
    104
    The BoxCollider2D always appears to maintain scale in the scene view, but then when the game is started, it reverts back to the original size. Same with the texture.
    If I disable the character controller script, gravity and collision will no longer be applied and I won't be able to test the collision anyway.
    I have a feeling the problem is inside the character controller script, but I just don't understand how. Nowhere am I changing the values of the scale of either the collider or the texture.
     
  10. Vitor_r

    Vitor_r

    Joined:
    May 23, 2013
    Posts:
    93
    Post the character controller script please.
    And some screenshots of the problem if you can.
     
  11. Limeoats

    Limeoats

    Joined:
    Aug 6, 2014
    Posts:
    104
    I managed to solve the problem. It turns out that it wasn't the character controller at all. The problem existed in the one script that I was certain was not the problem the entire time. It turns out that for some reason, in my AI script, I was setting the scale back to 1 whenever the player re-spawns. It is considered a re-spawn the very first time the player spawns too, so it was doing it immediately every time I started the game.
    Once I removed that line, everything worked as expected.
    Thanks for your help!
     
  12. Vitor_r

    Vitor_r

    Joined:
    May 23, 2013
    Posts:
    93
    Glad you solved the problem.
    Like i said before:
     
  13. Limeoats

    Limeoats

    Joined:
    Aug 6, 2014
    Posts:
    104
    I was certain that I had tried disabling each one with different results. I went back and did it again yesterday, which is how I found the issue.
    Thanks again.