1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
|
// ----------------------------------------------------------------------------
// <copyright file="PhotonTransformView.cs" company="Exit Games GmbH">
// PhotonNetwork Framework for Unity - Copyright (C) 2018 Exit Games GmbH
// </copyright>
// <summary>
// Component to synchronize Transforms via PUN PhotonView.
// </summary>
// <author>developer@exitgames.com</author>
// ----------------------------------------------------------------------------
namespace Photon.Pun
{
using UnityEngine;
[AddComponentMenu("Photon Networking/Photon Transform View")]
[HelpURL("https://doc.photonengine.com/en-us/pun/v2/gameplay/synchronization-and-state")]
public class PhotonTransformView : MonoBehaviourPun, IPunObservable
{
private float m_Distance;
private float m_Angle;
private Vector3 m_Direction;
private Vector3 m_NetworkPosition;
private Vector3 m_StoredPosition;
private Quaternion m_NetworkRotation;
public bool m_SynchronizePosition = true;
public bool m_SynchronizeRotation = true;
public bool m_SynchronizeScale = false;
[Tooltip("Indicates if localPosition and localRotation should be used. Scale ignores this setting, and always uses localScale to avoid issues with lossyScale.")]
public bool m_UseLocal;
bool m_firstTake = false;
public void Awake()
{
m_StoredPosition = transform.localPosition;
m_NetworkPosition = Vector3.zero;
m_NetworkRotation = Quaternion.identity;
}
private void Reset()
{
// Only default to true with new instances. useLocal will remain false for old projects that are updating PUN.
m_UseLocal = true;
}
void OnEnable()
{
m_firstTake = true;
}
public void Update()
{
var tr = transform;
if (!this.photonView.IsMine)
{
if (m_UseLocal)
{
tr.localPosition = Vector3.MoveTowards(tr.localPosition, this.m_NetworkPosition, this.m_Distance * Time.deltaTime * PhotonNetwork.SerializationRate);
tr.localRotation = Quaternion.RotateTowards(tr.localRotation, this.m_NetworkRotation, this.m_Angle * Time.deltaTime * PhotonNetwork.SerializationRate);
}
else
{
tr.position = Vector3.MoveTowards(tr.position, this.m_NetworkPosition, this.m_Distance * Time.deltaTime * PhotonNetwork.SerializationRate);
tr.rotation = Quaternion.RotateTowards(tr.rotation, this.m_NetworkRotation, this.m_Angle * Time.deltaTime * PhotonNetwork.SerializationRate);
}
}
}
public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
{
var tr = transform;
// Write
if (stream.IsWriting)
{
if (this.m_SynchronizePosition)
{
if (m_UseLocal)
{
this.m_Direction = tr.localPosition - this.m_StoredPosition;
this.m_StoredPosition = tr.localPosition;
stream.SendNext(tr.localPosition);
stream.SendNext(this.m_Direction);
}
else
{
this.m_Direction = tr.position - this.m_StoredPosition;
this.m_StoredPosition = tr.position;
stream.SendNext(tr.position);
stream.SendNext(this.m_Direction);
}
}
if (this.m_SynchronizeRotation)
{
if (m_UseLocal)
{
stream.SendNext(tr.localRotation);
}
else
{
stream.SendNext(tr.rotation);
}
}
if (this.m_SynchronizeScale)
{
stream.SendNext(tr.localScale);
}
}
// Read
else
{
if (this.m_SynchronizePosition)
{
this.m_NetworkPosition = (Vector3)stream.ReceiveNext();
this.m_Direction = (Vector3)stream.ReceiveNext();
if (m_firstTake)
{
if (m_UseLocal)
tr.localPosition = this.m_NetworkPosition;
else
tr.position = this.m_NetworkPosition;
this.m_Distance = 0f;
}
else
{
float lag = Mathf.Abs((float)(PhotonNetwork.Time - info.SentServerTime));
this.m_NetworkPosition += this.m_Direction * lag;
if (m_UseLocal)
{
this.m_Distance = Vector3.Distance(tr.localPosition, this.m_NetworkPosition);
}
else
{
this.m_Distance = Vector3.Distance(tr.position, this.m_NetworkPosition);
}
}
}
if (this.m_SynchronizeRotation)
{
this.m_NetworkRotation = (Quaternion)stream.ReceiveNext();
if (m_firstTake)
{
this.m_Angle = 0f;
if (m_UseLocal)
{
tr.localRotation = this.m_NetworkRotation;
}
else
{
tr.rotation = this.m_NetworkRotation;
}
}
else
{
if (m_UseLocal)
{
this.m_Angle = Quaternion.Angle(tr.localRotation, this.m_NetworkRotation);
}
else
{
this.m_Angle = Quaternion.Angle(tr.rotation, this.m_NetworkRotation);
}
}
}
if (this.m_SynchronizeScale)
{
tr.localScale = (Vector3)stream.ReceiveNext();
}
if (m_firstTake)
{
m_firstTake = false;
}
}
}
}
}
|