OnBack
How do I use On Back
Below are practical examples compiled from projects for learning and reference purposes
Featured Snippets
Line | Code | Ex.. |
---|---|---|
83 | double interpolationTime = currentTime - interpolationBackTime; | 1 |
13 | public void OnBack() | 2 |
12 | public void OnBack() | 3 |
20 | public void OnBack() | 4 |
7 | public void OnBack() | 5 |
File name: CubeInter.cs
Copy
79 // This only runs where the component is enabled, which is only on remote peers (server/clients)
80 void Update()
81 {
82 double currentTime = PhotonNetwork.time;
83 double interpolationTime = currentTime - interpolationBackTime;
84 // We have a window of interpolationBackTime where we basically play
85 // By having interpolationBackTime the average ping, you will usually use interpolation.
86 // And only if no more data arrives we will use extrapolation
87
88 // Use interpolation
89 // Check if latest state exceeds interpolation time, if this is the case then
90 // it is too old and extrapolation should be used
91 if (m_BufferedState[0].timestamp > interpolationTime)
92 {
93 for (int i = 0; i < m_TimestampCount; i++)
94 {
95 // Find the state which matches the interpolation time (time+0.1) or use last state
96 if (m_BufferedState[i].timestamp <= interpolationTime || i == m_TimestampCount - 1)
97 {
98 // The state one slot newer (<100ms) than the best playback state
99 State rhs = m_BufferedState[Mathf.Max(i - 1, 0)];
100 // The best playback state (closest to 100 ms old (default time))
101 State lhs = m_BufferedState[i];
102
103 // Use the time between the two slots to determine if interpolation is necessary
104 double length = rhs.timestamp - lhs.timestamp;
105 float t = 0.0F;
106 // As the time difference gets closer to 100 ms t gets closer to 1 in
107 // which case rhs is only used
108 if (length > 0.0001)
109 t = (float)((interpolationTime - lhs.timestamp) / length);
110
111 // if t=0 => lhs is used directly
112 transform.localPosition = Vector3.Lerp(lhs.pos, rhs.pos, t);
113 transform.localRotation = Quaternion.Slerp(lhs.rot, rhs.rot, t);
114 return;
115 }
116 }
117 }
118 // Use extrapolation. Here we do something really simple and just repeat the last
119 // received state. You can do clever stuff with predicting what should happen.
120 else
121 {
122 State latest = m_BufferedState[0];
123
124 transform.localPosition = Vector3.Lerp(transform.localPosition, latest.pos, Time.deltaTime * 20 );
125 transform.localRotation = latest.rot;
126 }
127 }
OnBack 141 lượt xem
Gõ tìm kiếm nhanh...