/// <summary> /// When entering a zone, checks if it's a Gravity Zone, and if yes, sets gravity accordingly /// </summary> /// <param name="collider">Collider.</param> protected virtual void OnTriggerEnter2D(Collider2D collider) { // if this is not a gravity zone, we do nothing and exit GravityZone gravityZone = collider.gameObject.GetComponentNoAlloc <GravityZone> (); if ((gravityZone == null) || !SubjectToGravityZones) { return; } // if we've entered another zone before exiting the one we are in, we cache the previous one to prevent glitches later if (_currentGravityZone != null && _currentGravityZone != gravityZone) { _cachedGravityZone = _currentGravityZone; } // we store our new gravity zone _currentGravityZone = gravityZone; // if we're over our inactive buffer, we set it if (Time.time - _entryTimeStampZones > InactiveBufferDuration) { SetGravityZone(gravityZone); } }
/// <summary> /// Sets the specified gravity zone as the current one, applying its gravity properties to our character /// </summary> /// <param name="gravityZone">Gravity zone.</param> protected virtual void SetGravityZone(GravityZone gravityZone) { // we apply our inactive buffer duration _entryTimeStampZones = Time.time; // we override our gravity _gravityOverridden = true; _overrideGravityAngle = gravityZone.GravityDirectionAngle; _inAGravityZone = true; StartRotating(); // we transition our character's state Transition(true, gravityZone.GravityDirectionVector); }
/// <summary> /// When exiting a gravity zone we reset our gravity and handle transition /// </summary> /// <param name="gravityZone">Gravity zone.</param> protected virtual void ExitGravityZone(GravityZone gravityZone) { _entryTimeStampZones = Time.time; // we reset our gravity _gravityOverridden = false; _inAGravityZone = false; StartRotating(); // we transition our character's state if (gravityZone) { Transition(false, gravityZone.GravityDirectionVector); } }
/// <summary> /// When exiting a zone, checks if it was a Gravity Zone, and if yes, resets gravity accordingly /// </summary> /// <param name="collider">Collider.</param> protected virtual void OnTriggerExit2D(Collider2D collider) { // if this is not a gravity zone, we do nothing and exit GravityZone gravityZone = collider.gameObject.GetComponentNoAlloc <GravityZone> (); if ((gravityZone == null) || !SubjectToGravityZones) { return; } // if the zone we are leaving is the one we had cached, we reset our stored grav zone if (gravityZone == _cachedGravityZone) { _cachedGravityZone = null; } // if the zone we are leaving is the current active one or if we have one in cache, we don't trigger exit for this zone or apply the cached gravity if (_currentGravityZone != gravityZone || _cachedGravityZone != null) { if (_cachedGravityZone != null) { _currentGravityZone = _cachedGravityZone; if (Time.time - _entryTimeStampZones > InactiveBufferDuration) { SetGravityZone(_currentGravityZone); } } return; } // we're not in a gravity zone anymore _currentGravityZone = null; // we apply our inactive buffer duration if (Time.time - _entryTimeStampZones > InactiveBufferDuration) { ExitGravityZone(gravityZone); } }