/// --------------------------------------------- /// Ultimate Character Controller /// Copyright (c) Opsive. All Rights Reserved. /// https://www.opsive.com /// --------------------------------------------- namespace Opsive.UltimateCharacterController.Objects.CharacterAssist { using Opsive.UltimateCharacterController.Character; using Opsive.UltimateCharacterController.Character.Abilities; using Opsive.UltimateCharacterController.Game; using Opsive.UltimateCharacterController.Utility; using UnityEngine; /// /// A Gravity Zone represents a trigger area that adjusts the character's gravity direction when the character is within the trigger. /// public abstract class GravityZone : MonoBehaviour { /// /// Determines the direction of gravity that should be applied. /// /// The position of the character. /// The direction of gravity that should be applied. public abstract Vector3 DetermineGravityDirection(Vector3 position); /// /// An object has entered the trigger. /// /// The object that entered the trigger. private void OnTriggerEnter(Collider other) { // A main character collider is required. if (!MathUtility.InLayerMask(other.gameObject.layer, 1 << LayerManager.Character)) { return; } // The object must be a character. var characterLocomotion = other.GetComponentInParent(); if (characterLocomotion == null) { return; } // With the Align To Gravity Zone ability. var alignToGravity = characterLocomotion.GetAbility(); if (alignToGravity == null) { return; } alignToGravity.RegisterGravityZone(this); } /// /// An object has exited the trigger. /// /// The collider that exited the trigger. private void OnTriggerExit(Collider other) { // A main character collider is required. if (!MathUtility.InLayerMask(other.gameObject.layer, 1 << LayerManager.Character)) { return; } // The object must be a character. var characterLocomotion = other.GetComponentInParent(); if (characterLocomotion == null) { return; } // With the Align To Gravity Zone ability. var alignToGravity = characterLocomotion.GetAbility(); if (alignToGravity == null) { return; } alignToGravity.UnregisterGravityZone(this); } } }