How to tell if center of character controller is within a trigger

Hey guys- I have a trigger in my 3D game. When I enter the trigger, I set a bool called canShrink to true. Then when I hold Shift, my character (and character controller capsule) shrinks. The code is like this:
void OnTriggerStay (Collider other)
{
if (other.tag == “ShrinkArea”) {
canShrink = true;
}
}

		void OnTriggerExit (Collider other)
		{
			canShrink = false;
		}
void Update ()
		{
		           if (Input.GetKey(KeyCode.LeftShift)){
                                     Shrink();
                          }
		}

		// This is called by MyPlayer upon groundswim input
        public void Shrink ()
		{
			//if (canShrink) {
				//if (CurrentMovementState == DefaultMovementState) {
					//this sets our character controller capsule dimensions to be very small
					this.GetComponent<KinematicCharacterMotor> ().SetCapsuleDimensions (.1f, .2f, .1f);
					//this turns off our playerSphere mesh renderer
					playerMesh.GetComponent<MeshRenderer> ().enabled = false;	
					//This transitions us to Shrunken State
					TransitionToState (ShrinkState);
				//}
			//} else if (!canShrink) {
				//ExitShrink();
			//}
        }

        public void ExitShrink()
		{
			if (CurrentMovementState == ShrinkState)
            {
            	// this resets our capsule to the size it was at Start()
				this.GetComponent<KinematicCharacterMotor>().SetCapsuleDimensions(originalCharacterRadius, originalCharacterHeight, originalCharacterHeight*.5f);
				//this transitions us back to default movement state
                TransitionToState(DefaultMovementState);
                //this re-enables our mesh renderer on player geometry
				playerMesh.GetComponent<MeshRenderer>().enabled = true;	
            }
		}

The issue I’m getting is, when I hold shift and my character shrinks, they are then outside of the “CanShrink” trigger zone (because the character controller capsule shrank, so then they can’t shrink, and return to normal size, at which point they “Canshrink” and shrink again, and so on and so forth.
Does anyone know how to check if the CENTER of my character is within a trigger? This way, it will consistently be within the trigger whether I am small, or large

There is always more than one way to do a task in programming, especially if you use good programming logic. There are mathematical ways to track an object’s center, there’s ways to track the center built-in to Unity. You could just add a empty game-object(as a child of the Player) and use it as a marker, that way it can always be put where you need it no matter what size your character is. Name it something like “CharCenter”, you could even attach a collider to CharCenter and track it’s collisions. For example, if the CharCenter is at the center of your character object, and is still colliding, then…


A different approach would be to make a collider(as a child of Player) that is the same shape, size and position as your original (non-shrunken) character collider. That way, when you shrink the character and it’s main collider, the CharCenter collider (the same shape and size of the original) is still there and, as you said, it will consistently be within the trigger whether you are small, or large.

You might want to disable the ‘shrink-collider’ from script after it is triggered, then set a timer (either through Update or IEnumerator) that re-enables the collider again.


See, there are probably lots of ways to do this, you just have to decide which gives you closest to the results you want and which goes the best with the rest of your program. Sometimes the first way we try things doesn’t turn out. You might want to start over and try a different approach. Just think logically about what you want to accomplish, step-by-step. For example:


“I only want the player to be able to shrink ONLY when they are within the ‘shrink-zone’ (collider), and ONLY when they press SHIFT”. In other words, “ONLY when they are in the ‘shrink-zone’ AND pressing SHIFT”.

Think about the different ways you can accomplish that without getting too complicated. Unity has many built-in functions and variables for data-tracking without having to add extra scripts or complicated custom class systems.

You could even check if the Player is in the ‘shrink-zone’ by using position distance instead of colliders as triggers. You might find that more suitable.
For example:

//have this on Player Object

public Transform ShrinkZone1;

…then in Update():

 float dist = Vector3.Distance(ShrinkZone1.position, transform.position);
 print("Distance to shrinker: " + dist); //debugging

I mention all this, because you can just do a distance check to see if your Player is within the ‘shrink-zone’, you don’t need Unity to check everything, it can save Unity’s collision-checking power and resources for more important collisions in your project, little things like that can speed up a slow-running build.

You could add this:

float TrigAmount = 2.0f;
if (dist <= TrigAmount) {canShrink = true;}

So, experiment. You’ll learn about the pro’s and con’s of different methods in Unity.