How to auto adjust the collider to the sprites size.

When designing a 2D level we usually need to resize our sprites to match the design and the difficulty level that we want to have. Also we need to be able to adjust it based on the feedback from the players:

We can easily adjust the size of a sprite using the Rect tool if we 9-slice it and if we use SpriteRenderer in Tiled mode.

We can easily adjust the size of a sprite using the Rect tool if we 9-slice it and if we use SpriteRenderer in Tiled mode.

 


The setup:

9-slicing is a 2D technique which allows you to reuse an image at various sizes without needing to prepare multiple Assets.

Tiled mode - “By default, this mode causes the middle of the 9-Sliced Sprite to tile instead of scale when its dimensions change.”source: SpriteRenderer in Tiled mode

Tiled mode - “By default, this mode causes the middle of the 9-Sliced Sprite to tile instead of scale when its dimensions change.”

source: SpriteRenderer in Tiled mode

 

Ok now we can adjust the size as we want. But is the sprites size the only concern?

 


Problem with the collider

The problem is that the sprite needs to have a collider so that our agents can use them. Also the collider needs to be adjusted to the sprite to avoid some strange bugs in our game.

Lucky for us we can ensure that all colliders adjust their size dynamically at the start of the game. All we need is a script attached to our platform ex PlatformColliderAdjustment.cs :

 
public class PlatformColliderAdjustment : MonoBehaviour { [SerializeField] private BoxCollider2D platformCollider; [SerializeField] private SpriteRenderer spriteRenderer; private void Start() { platformCollider.size = new Vector2(spriteRenderer.size.x, platformCollider.size.y); } }

All we need to do is to set the size of the collider to a new Vector2. In my case I only worry about X axis its using the size of the spriteRenderer.size.x while Y axis stays the same (platformCollider.size.y).

Keep in mind that most child classes of Collider2D has the size parameter, but the base class has only Bounds struct assigned to it.

Now the script will adust the colliders size at runtime.

Now the script will adjust the colliders size at runtime.

 
Our dinosaur can finally avoid fallings into its own demise at the start of our level.

Our dinosaur can finally avoid fallings into its own demise at the start of our level.

 

Want to learn more about Unity 2D?

The above solution is from my video course about making a 2D platformer game in Unity 2020. If you want to learn more about making 2D games and at the same time help me to make more free learning materials check it out here: Make a 2D platformer using Design patterns

Learn how to make a 2D platformer and how to use Design patterns to create a well architected code base that you can easily extend with new game mechanics. It will really allow you to make fun when making a game instead of worrying about your new co…

Learn how to make a 2D platformer and how to use Design patterns to create a well architected code base that you can easily extend with new game mechanics. It will really allow you to make fun when making a game instead of worrying about your new code breaking your old game mechanics.

 
 
 
 

PS. Here is the code that I use to display a gizmo over the collider:

 
public class ColliderGizmo : MonoBehaviour { public BoxCollider2D colliderbox; public Color gizmoColor = Color.green; private void OnDrawGizmos() { if (colliderbox == null) return; Gizmos.color = gizmoColor; Gizmos.DrawCube(colliderbox.bounds.center, colliderbox.bounds.size); } }
 

If you enjoy this post please consider supporting me through Pateron

 
 

You can also check my other tutorials on my YT channel:

 

If you agree or disagree let me know by joining the Sunny Valley Studio discord channel :)

Thanks for reading!

Peter

Previous
Previous

How to remove tears appearing between tiles

Next
Next

How to sort sprites by Y axis in Unity 2D