/// --------------------------------------------- /// Ultimate Character Controller /// Copyright (c) Opsive. All Rights Reserved. /// https://www.opsive.com /// --------------------------------------------- namespace Opsive.UltimateCharacterController.Game { using UnityEngine; /// /// The Kinematic Object component allows an object to be moved outside of the Kinematic Object Manager loop while still being tracked by the Kinematic Object Manager. /// This component should be used with the Move With Object ability: /// https://opsive.com/support/documentation/ultimate-character-controller/character/abilities/included-abilities/move-with-object/ /// public class KinematicObject : MonoBehaviour, IKinematicObject { private Transform m_Transform; private Vector3 m_LastPosition; private Quaternion m_LastRotation; private int m_KinematicObjectIndex; public int KinematicObjectIndex { set { m_KinematicObjectIndex = value; } } public KinematicObjectManager.UpdateLocation UpdateLocation { get { return KinematicObjectManager.UpdateLocation.FixedUpdate; } } /// /// Initialize the default values. /// private void Awake() { m_Transform = transform; } /// /// Registers the object with the Kinematic Object Manager. /// public void OnEnable() { m_KinematicObjectIndex = KinematicObjectManager.RegisterKinematicObject(this); m_LastPosition = m_Transform.position; m_LastRotation = m_Transform.rotation; } /// /// Updates the position/rotation of the object. The Kinematic Object component should execute after the object has been moved. /// public void FixedUpdate() { m_LastPosition = m_Transform.position; m_LastRotation = m_Transform.rotation; } /// /// Sets up the object to be moved by the Kinematic Object Manager. /// public void Move() { m_Transform.position = m_LastPosition; m_Transform.rotation = m_LastRotation; } /// /// Unregisters the object with the Kinematic Object Manager. /// public void OnDisable() { KinematicObjectManager.UnregisterKinematicObject(m_KinematicObjectIndex); } } }