Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by SomeGuy22 · Aug 04, 2012 at 03:59 PM · networkingpositionresetinterpolationexample

Network Interpolated Transform resets position?

I have a script that activates the network interpolated transform script from the Networking Example, but whenever it activates, the network interpolation script seems to just reset the position to 0, and not where the object should be.

Does any one know what is causing the reset?

Here's the code by the way:

 using UnityEngine;
 using System.Collections;
 
 public class NetworkInterpolatedTransform : MonoBehaviour {
  
     public double interpolationBackTime = 0.1; 
     
     internal struct State
     {
         internal double timestamp;
         internal Vector3 pos;
         internal Quaternion rot;
     }
     
     // We store twenty states with "playback" information
     State[] m_BufferedState = new State[20];
     
     // Keep track of what slots are used
     int m_TimestampCount;
     
     void OnSerializeNetworkView(BitStream stream, NetworkMessageInfo info)
     {
         // Always send transform (depending on reliability of the network view)
         if (stream.isWriting)
         {
             Vector3 pos = transform.localPosition;
             Quaternion rot = transform.localRotation;
             stream.Serialize(ref pos);
             stream.Serialize(ref rot);
         }
         // When receiving, buffer the information
         else
         {
             // Receive latest state information
             Vector3 pos = transform.position;
             Quaternion rot = Quaternion.identity;
             stream.Serialize(ref pos);
             stream.Serialize(ref rot);
             
             // Shift buffer contents, oldest data erased, 18 becomes 19, ... , 0 becomes 1
             for (int i = m_BufferedState.Length-1; i >= 1; i--)
             {
                 m_BufferedState[i] = m_BufferedState[i-1];
             }
             
             // Save currect received state as 0 in the buffer, safe to overwrite after shifting
             State state;
             state.timestamp = info.timestamp;
             state.pos = pos;
             state.rot = rot;
             m_BufferedState[0] = state;
             
             // Increment state count but never exceed buffer size
             m_TimestampCount = Mathf.Min(m_TimestampCount + 1, m_BufferedState.Length);
             
             // Check integrity, lowest numbered state in the buffer is newest and so on
             for (int i = 0; i < m_TimestampCount-1; i++)
             {
                 if (m_BufferedState[i].timestamp < m_BufferedState[i+1].timestamp)
                 Debug.Log("State inconsistent");
             }
             
             //Debug.Log("stamp: " + info.timestamp + "my time: " + Network.time + "delta: " + (Network.time - info.timestamp));
         }
     }
     
     // This only runs where the component is enabled, which is only on remote peers (server/clients)
     void Update ()
     {
         double currentTime = Network.time;
         double interpolationTime = currentTime - interpolationBackTime;
         // We have a window of interpolationBackTime where we basically play 
         // By having interpolationBackTime the average ping, you will usually use interpolation.
         // And only if no more data arrives we will use extrapolation
         
         // Use interpolation
         // Check if latest state exceeds interpolation time, if this is the case then
         // it is too old and extrapolation should be used
         if (m_BufferedState[0].timestamp > interpolationTime)
         {
             for (int i = 0; i < m_TimestampCount; i++)
             {
                 // Find the state which matches the interpolation time (time+0.1) or use last state
                 if (m_BufferedState[i].timestamp <= interpolationTime || i == m_TimestampCount-1)
                 {
                     // The state one slot newer (<100ms) than the best playback state
                     State rhs = m_BufferedState[Mathf.Max(i-1, 0)];
                     
                     // The best playback state (closest to 100 ms old (default time))
                     State lhs = m_BufferedState[i];
                     
                     // Use the time between the two slots to determine if interpolation is necessary
                     double length = rhs.timestamp - lhs.timestamp;
                     float t = 0.0F;
                     // As the time difference gets closer to 100 ms t gets closer to 1 in 
                     // which case rhs is only used
                     if (length > 0.0001)
                         t = (float)((interpolationTime - lhs.timestamp) / length);
                     
                     // if t=0 => lhs is used directly
                     transform.localPosition = Vector3.Lerp(lhs.pos, rhs.pos, t);
                     transform.localRotation = Quaternion.Slerp(lhs.rot, rhs.rot, t);
                     return;
                 }
             }
         }
         // Use extrapolation. Here we do something really simple and just repeat the last
         // received state. You can do clever stuff with predicting what should happen.
         else
         {
             State latest = m_BufferedState[0];
             transform.localPosition = latest.pos;
             transform.localRotation = latest.rot;
         }
     }
 }

Sorry if it's long!

Comment
Add comment · Show 22
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ScroodgeM · Aug 05, 2012 at 12:55 PM 2
Share

insert Debug.Log() to OnSerializeNetworkView method to monitor which data is sended and received.

avatar image SomeGuy22 · Aug 05, 2012 at 10:45 PM 0
Share

That's really strange... the data I'm sending is correct, but the data I'm receiving is completely wrong. What's going on here?

avatar image ScroodgeM · Aug 06, 2012 at 04:43 PM 0
Share

do you check this data between sender and receiver? or something in between? for example, if you test data sending by client and receiving by another client, it could be changed by server those clients connected to.

avatar image SomeGuy22 · Aug 06, 2012 at 05:21 PM 0
Share

? I don't understand?

avatar image ScroodgeM · Aug 06, 2012 at 07:59 PM 0
Share

describe you application stucture please. clients with single unplayable server, or server is one of clients, or something? where exactly in this model did you check sending and receiving?

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by SomeGuy22 · Aug 07, 2012 at 11:19 PM

ScroodgeM:

inerpolater transform script should apply data on server immediately, and use interpolation on clients only.

SomeGuy22:

Oh. So then I'd have to write a custom transform sync script for them?

ScroodgeM:

"methinks it's always the best way to achieve what you want 8)

but in your case if you want not to write custom sync, you should just avoid this position losings...

interpolater transform should do some another work on server. it should receive and store data in some variables, and on sending data just send these stored data. in this way you needn't move object on server. but really more comfortable to make the same scene on server and have no problems at all 8)"

EDIT: Unfortunately the same glitch is happening: the data being received is completely wrong even with a custom script. Anyone have any ideas?

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

NetworkTransform not interpolating? 0 Answers

Get Interpolated rigidbody velocity 1 Answer

Strange Variables in OnSerializeNetworkView? 2 Answers

LobbyManager / NetworkManager doesn't reset properly 4 Answers

How Push Players on Network 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges