-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNetworkTransformSync.cs
More file actions
41 lines (37 loc) · 1.13 KB
/
NetworkTransformSync.cs
File metadata and controls
41 lines (37 loc) · 1.13 KB
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
using Photon.Pun;
using UnityEngine;
public class NetworkTransformSync : MonoBehaviour, IPunObservable
{
private PhotonView photonView;
private Vector3 networkPosition;
private Quaternion networkRotation;
void Awake()
{
photonView = GetComponent<PhotonView>();
if (photonView == null)
{
photonView = gameObject.AddComponent<PhotonView>();
}
}
void Update()
{
if (!photonView.IsMine)
{
transform.position = Vector3.Lerp(transform.position, networkPosition, Time.deltaTime * 10);
transform.rotation = Quaternion.Lerp(transform.rotation, networkRotation, Time.deltaTime * 10);
}
}
public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
{
if (stream.IsWriting)
{
stream.SendNext(transform.position);
stream.SendNext(transform.rotation);
}
else
{
networkPosition = (Vector3)stream.ReceiveNext();
networkRotation = (Quaternion)stream.ReceiveNext();
}
}
}