I got a character that has a rolling animation. I want to make the collision box smaller on that animation (so that the character can roll under objects). How can I change the collision box in that specific animation?
This is all in 2D.
*** I’m using the new 2D animator, so I don’t use any scripts for the animation.
Either modify the size of the collision model over time to roughly match the animation, or do a quick swap (either by changing the size of the collider during just one frame, or having 2 colliders and enabling / disabling them to make them the collider your GameObject uses). When increasing the size of a collider, you’ll usually also want to check to see if there’s space to increase its size, or you’ll get unwanted things happening with your physics (eg, getting stuck, popping through walls, flying rapidly away from somewhere you get jammed in then pop out of).
An example of the quick swap method with a single BoxCollider2D would be:
public float crouchingHeight = 0.8f;
public float standingHeight = 1.6f;
public BoxCollider2D myCollider; //assign this in the Inspector tab in the Editor
public void DoRollAnimation() {
Invoke("StopRoll",animation.clip.length);
Vector2 temp = myCollider.size;
temp.y = crouchingHeight;
myCollider.size = temp;
//might also want to set myCollider.center to move the collider down from standing center
}
}
public void StopRoll() {
//might want to check here to ensure you
//have clear space above the player to stand
isCrouching = false;
Vector2 temp = myCollider.size;
temp.y = standingHeight;
myCollider.size = temp;
//might also want to set myCollider.center to move the collider up from crouching center
}