/// --------------------------------------------- /// Ultimate Character Controller /// Copyright (c) Opsive. All Rights Reserved. /// https://www.opsive.com /// --------------------------------------------- namespace Opsive.UltimateCharacterController.Demo.Objects { using Opsive.UltimateCharacterController.Character; using Opsive.UltimateCharacterController.Character.Abilities; using Opsive.UltimateCharacterController.Game; using Opsive.UltimateCharacterController.Utility; using UnityEngine; /// /// Moves the platform between two points. This gives an example of using the Move With Object ability and moving on an object that is /// updated outside of the Kinematic Object Manager update loop. /// public class SimplePlatform : MonoBehaviour { [Tooltip("The position that the platform should move towards when the character is not on top of it.")] [SerializeField] protected Vector3 m_RestingPosition; [Tooltip("The position that the platform should move towards when the character is on top of it.")] [SerializeField] protected Vector3 m_ActivePosition; [Tooltip("The speed that the platform should move.")] [SerializeField] protected float m_MoveSpeed = 0.05f; private Transform m_Transform; private MoveWithObject m_MoveWithObjectAbility; /// /// Initialize the default values. /// private void Awake() { m_Transform = transform; } /// /// Teleport the character to the specified destination. /// /// The collider that entered the trigger. May or may not be a character. private void OnTriggerEnter(Collider other) { if (!MathUtility.InLayerMask(other.gameObject.layer, 1 << LayerManager.Character)) { return; } UltimateCharacterLocomotion characterLocomotion; if ((characterLocomotion = other.GetComponentInParent()) == null) { return; } m_MoveWithObjectAbility = characterLocomotion.GetAbility(); if (m_MoveWithObjectAbility == null) { return; } m_MoveWithObjectAbility.Target = m_Transform; } /// /// Moves the platform. /// private void FixedUpdate() { // Move towards the active position when the ability reference is not null. This will be set within OnTriggerEnter/Exit. m_Transform.position = Vector3.MoveTowards(m_Transform.position, (m_MoveWithObjectAbility != null ? m_ActivePosition : m_RestingPosition), m_MoveSpeed); } /// /// An object has exited the trigger. /// /// The collider that entered the trigger. May or may not be a character. private void OnTriggerExit(Collider other) { if (!MathUtility.InLayerMask(other.gameObject.layer, 1 << LayerManager.Character) || m_MoveWithObjectAbility == null) { return; } m_MoveWithObjectAbility.Target = null; m_MoveWithObjectAbility = null; } } }