blob: ba6333dbe1d828779773693aac75f4241c2746ef (
plain)
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
|
// ----------------------------------------------------------------------------
// <copyright file="PhotonRigidbody2DView.cs" company="Exit Games GmbH">
// PhotonNetwork Framework for Unity - Copyright (C) 2018 Exit Games GmbH
// </copyright>
// <summary>
// Component to synchronize 2d rigidbodies via PUN.
// </summary>
// <author>developer@exitgames.com</author>
// ----------------------------------------------------------------------------
namespace Photon.Pun
{
using UnityEngine;
[RequireComponent(typeof(Rigidbody2D))]
[AddComponentMenu("Photon Networking/Photon Rigidbody 2D View")]
public class PhotonRigidbody2DView : MonoBehaviourPun, IPunObservable
{
private float m_Distance;
private float m_Angle;
private Rigidbody2D m_Body;
private Vector2 m_NetworkPosition;
private float m_NetworkRotation;
[HideInInspector]
public bool m_SynchronizeVelocity = true;
[HideInInspector]
public bool m_SynchronizeAngularVelocity = false;
[HideInInspector]
public bool m_TeleportEnabled = false;
[HideInInspector]
public float m_TeleportIfDistanceGreaterThan = 3.0f;
public void Awake()
{
this.m_Body = GetComponent<Rigidbody2D>();
this.m_NetworkPosition = new Vector2();
}
public void FixedUpdate()
{
if (!this.photonView.IsMine)
{
this.m_Body.position = Vector2.MoveTowards(this.m_Body.position, this.m_NetworkPosition, this.m_Distance * (1.0f / PhotonNetwork.SerializationRate));
this.m_Body.rotation = Mathf.MoveTowards(this.m_Body.rotation, this.m_NetworkRotation, this.m_Angle * (1.0f / PhotonNetwork.SerializationRate));
}
}
public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
{
if (stream.IsWriting)
{
stream.SendNext(this.m_Body.position);
stream.SendNext(this.m_Body.rotation);
if (this.m_SynchronizeVelocity)
{
stream.SendNext(this.m_Body.velocity);
}
if (this.m_SynchronizeAngularVelocity)
{
stream.SendNext(this.m_Body.angularVelocity);
}
}
else
{
this.m_NetworkPosition = (Vector2)stream.ReceiveNext();
this.m_NetworkRotation = (float)stream.ReceiveNext();
if (this.m_TeleportEnabled)
{
if (Vector3.Distance(this.m_Body.position, this.m_NetworkPosition) > this.m_TeleportIfDistanceGreaterThan)
{
this.m_Body.position = this.m_NetworkPosition;
}
}
if (this.m_SynchronizeVelocity || this.m_SynchronizeAngularVelocity)
{
float lag = Mathf.Abs((float)(PhotonNetwork.Time - info.SentServerTime));
if (m_SynchronizeVelocity)
{
this.m_Body.velocity = (Vector2)stream.ReceiveNext();
this.m_NetworkPosition += this.m_Body.velocity * lag;
this.m_Distance = Vector2.Distance(this.m_Body.position, this.m_NetworkPosition);
}
if (this.m_SynchronizeAngularVelocity)
{
this.m_Body.angularVelocity = (float)stream.ReceiveNext();
this.m_NetworkRotation += this.m_Body.angularVelocity * lag;
this.m_Angle = Mathf.Abs(this.m_Body.rotation - this.m_NetworkRotation);
}
}
}
}
}
}
|