Ejemplo n.º 1
0
        internal void ReverseTransform(float secondsAgo)
        {
            savedPosition = transform.position;
            savedRotation = transform.rotation;

            float currentTime = NetworkManager.Singleton.NetworkTime;
            float targetTime  = currentTime - secondsAgo;

            float previousTime = 0f;
            float nextTime     = 0f;

            for (int i = 0; i < m_Framekeys.Count; i++)
            {
                if (previousTime <= targetTime && m_Framekeys.ElementAt(i) >= targetTime)
                {
                    nextTime = m_Framekeys.ElementAt(i);
                    break;
                }
                else
                {
                    previousTime = m_Framekeys.ElementAt(i);
                }
            }
            float timeBetweenFrames    = nextTime - previousTime;
            float timeAwayFromPrevious = currentTime - previousTime;
            float lerpProgress         = timeAwayFromPrevious / timeBetweenFrames;

            transform.position = Vector3.Lerp(m_FrameData[previousTime].position, m_FrameData[nextTime].position, lerpProgress);
            transform.rotation = Quaternion.Slerp(m_FrameData[previousTime].rotation, m_FrameData[nextTime].rotation, lerpProgress);
        }
        /// <summary>
        ///     <para>
        ///         Sets the <see cref="GameObject" />s <see cref="Transform" /> position and rotation using the given frame index,
        ///         and optional interpolation value.
        ///     </para>
        ///     <para>
        ///         The <see cref="Transform" /> will not be changed back until a call to <see cref="ResetStateTransform" />
        ///     </para>
        /// </summary>
        /// <param name="secondsAgo"></param>
        public void SetStateTransform(double secondsAgo)
        {
            Transform objTransform = transform;

            //Save last state
            lastPosition = objTransform.position;
            lastRotation = objTransform.rotation;

            double currentTime = NetworkTime.time;
            double targetTime  = currentTime - secondsAgo;

            double previousTime = 0f;
            double nextTime     = 0f;

            for (int i = 0; i < frameKeys.Count; i++)
            {
                if (previousTime <= targetTime && frameKeys.ElementAt(i) >= targetTime)
                {
                    nextTime = frameKeys.ElementAt(i);
                    break;
                }
                else
                {
                    previousTime = frameKeys.ElementAt(i);
                }
            }

            if (nextTime == 0)
            {
                nextTime = frameKeys.GetMostRecentElement();
            }

            double timeBetweenFrames    = nextTime - previousTime;
            double timeAwayFromPrevious = currentTime - previousTime;

            //We loose some accuracy here, but Unity's transforms are floats
            float lerpProgress = (float)(timeAwayFromPrevious / timeBetweenFrames);

            Logger.Debug("TimeAgo: {TimeAgo}, previousTime: {PreviousTime}, nextTime: {NextTime}, lerp: {Lerp}",
                         secondsAgo, previousTime, nextTime, lerpProgress);

            objTransform.position =
                Vector3.Lerp(frameData[previousTime].Position, frameData[nextTime].Position, lerpProgress);
            objTransform.rotation = Quaternion.Slerp(frameData[previousTime].Rotation, frameData[nextTime].Rotation,
                                                     lerpProgress);
        }