A common setup for this scenarioIt is HIGHLY inadvisable to separate completely your physics logic from your damage logicmutate a collider during runtime. Start by adding 2 CollidersThis will lead to your charactermany many bugs and weird physics artifacts. One Collider will be forNot to mention it puts strain on the physics system, and will remain inas a default state. The other Collider will be called your HurtBoxlot of the broad phase and will havenarrow phase cullings can rely on the isTrigger box checked. The triggershape of the collider being cached. Changing the shape will not react withrequire the physics, but will throw a "OnTriggerEnter", "OnTriggerStay" system to re evaluate these entries. My recommendation is to have two colliders that you toggle between. Simple disable your standing collider, and "OnTriggerExit" call on whichever objectenable your rigidbody is onrolling collider. The structure I would recommend isHowever we can go further to increase the safety of what you're trying to do. Imagine the following setup:
PlayerGameObject
{} PlayerController.cs
-Character [Rigidbody,CollidersGameObject
- StandingBodyColliderGameObject - Layer: "Default"
{} Collider,
Controller, Health] - RollingBodyColliderGameObject - Layer: "Default"
{} Collider (Rolling) // Disabled
-HurtBox [Trigger,RollUnderColliderGameObject HurtBox]- Layer: "Roll Under"
{} Collider (Same as standing but only collides with Roll Under)
RigidbodyThe idea here is you have your standing and rolling hitboxes for physical interactions with other things in the rigidbody ofgame. However in your charactercollision matrix, Collider ismake it so the "Roll Under" layer only collides with itself. This means your normal collider that will react to physics mentioned above. Controller is responsible AND ONLY responsible for handling movementpass through and character state. It should handle your "Dodge Roll"surfaces you could roll under. Health is a script responsible AND ONLY responsible for handlingHowever if you have your current health. This includes receiving damage. Now in the child gameobjectRollUnderCollider enabled, Trigger is the trigger thatit will collide with things that can damage yourthose surfaces and stop the player. You may wish to extend this model from being able to have several hurt boxes, itmove through. When you want damage to be more precise onroll, simply disable this collider. This will let your character phase through. Or this can be as simple as a capsule trigger that matchesIf you want your physics Colliderplayer's standard hitbox to also shrink, disable the standing body collider and enable rolling body collider. Your HurtBoxHowever this rolling body collider will have your OnTriggerEnter functionstill phase through the roll under. This function will work on the same object asmeans that if your collider OR onsurface is slightly slanted, or not the object ofexact right height, your rigidbodyroll will still work. But we'll put it on the childWhen your roll is over, check if there's a roll under object justabove you (you can use physics queries for organizationthis). The HurtBox script will decide what hits itIf not, and will tellrestore your Health script to receive a certain amount of damageRollUnderCollider. For example
public Health AffectedHealth; // Set in editor
private void OnTriggerEnter(Collider otherCollider)
{
EnemyBullet bullet = otherCollider.GetComponent<EnemyBullet>();
if (bullet != null) {
AffectedHealth.Damage(bullet.DamageAmount);
}
}
Your Health script as you can seeAgain, will have a Damage function. Your Health script should also have a function called "MakeInvulnerable" and "MakeVulnerable". The final idea herethe reason for this multi collider setup is that your Controllerphysics performance, calls MakeInvulnerable on your health script when you rolledge case safety, and MakeVulnerable when the roll is doneless constraint on level design. Your hurtbox is constantly enabled and sending any damage requestsWhat you're trying to Healthdo isn't an easy problem, but Health itself is blocking it. Some cool things about this isand an easy solution will likely turn complex very quickly as you could use this information to track stats, like damage mitigated, damage taken, etcgo through edge cases. Since HealthThis is always receiving the damage function calls. You can also optimize this method by usingjust my recommendation from time in the collision matrixindustry and putting your hurtbox on it's own layer, so only certain collisions will registerdealing with itchanging colliders.