BoxCollider2D method

Instead of using the Capsule2D method, I instead opted to use BoxCollider2D as my preferred method of body collision. So when Rick brought this method to fix the wall jumping issues, I wanted to keep the BoxCollider2D. After a bit of research into the Unity Documentation, I came up with the follow solution:

I first created 2 children on the player game object, and named them “Body Collision” and “Feet Collision”. I then moved the original BoxCollider2D to the “Body Collision” child. I created a new BoxCollider2D under the “Feet Collision” and made it to be around the foot level.

Made a few tweaks to the code, specifically using the Transform.Find method. I renamed the “myCollider” variable into “bodyCollider” and created a new one named “feetCollier”. Once that was done, I went into Start() and deleted the original GetComponent(). I used the following method instead:

bodyCollider = gameObject.transform.Find("Body Collision").GetComponent<BoxCollider2D>();

Did a similar thing to the “feetCollider” variable with my final code below:

// Component Initializers
Rigidbody2D myRigidbody;
Animator myAnimator;
BoxCollider2D bodyCollider;
BoxCollider2D feetCollider;

float defaultGravity;

void Start()
{
    // Assign the variables to the components
    myRigidbody = GetComponent<Rigidbody2D>();
    myAnimator = GetComponent<Animator>();
    bodyCollider = gameObject.transform.Find("Body Collision").GetComponent<BoxCollider2D>();
    feetCollider = gameObject.transform.Find("Feet Collision").GetComponent<BoxCollider2D>();

    defaultGravity = myRigidbody.gravityScale;
}

Made the changes like Rick did in the lecture and everything works perfectly! Allows me to continue using BoxCollider2D and helped me learn something new! Maybe some of you guys might use this as well if you want to use BoxCollider2D!

Privacy & Terms