Home > Software engineering >  How to change the size of a CapsuleCollider2D with code, Unity C#
How to change the size of a CapsuleCollider2D with code, Unity C#

Time:09-21

my character has a CapsuleCollider2D and now I want to change the height of the collider when he crouches. I found different ways for BoxColliders2D and normal CapsuleCollider 3D but not for the 2D version. Thanks in advance

~Julian

CodePudding user response:

You can access the capsule collider size with:

myCapsuleCollider.size

You can change it like so:

myCapsuleCollider.size = new Vector2(desiredWidth,desiredHeight);

Just replace desiredWidth and desiredHeight with the values you want the character collider to resize to.

Unity Docs: https://docs.unity3d.com/Manual/class-CapsuleCollider2D.html

  • Related