@@ -17,29 +17,87 @@ public override void OnNetworkSpawn()
17
17
m_Rigidbody = GetComponent < Rigidbody > ( ) ;
18
18
if ( NetworkObject != null && m_Rigidbody != null )
19
19
{
20
- m_Rigidbody . isKinematic = ! NetworkObject . IsOwner ;
21
- if ( ! m_Rigidbody . isKinematic )
20
+ if ( NetworkObject . IsOwner )
22
21
{
23
22
ChangeDirection ( true , true ) ;
24
23
}
25
24
}
26
25
}
27
26
27
+ /// <summary>
28
+ /// Notify the server of any client side change in direction or speed
29
+ /// </summary>
30
+ /// <param name="moveTowards"></param>
31
+ [ ServerRpc ( RequireOwnership = false ) ]
32
+ private void MovePlayerServerRpc ( Vector3 moveTowards )
33
+ {
34
+ m_MoveTowardsPosition = moveTowards ;
35
+ }
36
+
37
+ private Vector3 m_MoveTowardsPosition ;
38
+
28
39
public void Move ( int speed )
29
40
{
30
- m_Rigidbody . MovePosition ( transform . position + m_Direction * ( speed * Time . fixedDeltaTime ) ) ;
41
+ // Server sets this locally
42
+ if ( IsServer && IsOwner )
43
+ {
44
+ m_MoveTowardsPosition = ( m_Direction * speed ) ;
45
+ }
46
+ else if ( ! IsServer && IsOwner )
47
+ {
48
+ // Client must sent Rpc
49
+ MovePlayerServerRpc ( m_Direction * speed ) ;
50
+ }
51
+ }
52
+
53
+ // We just apply our current direction with magnitude to our current position during fixed update
54
+ private void FixedUpdate ( )
55
+ {
56
+ if ( IsServer && NetworkObject && NetworkObject . NetworkManager && NetworkObject . NetworkManager . IsListening )
57
+ {
58
+ if ( m_Rigidbody == null )
59
+ {
60
+ m_Rigidbody = GetComponent < Rigidbody > ( ) ;
61
+ }
62
+ if ( m_Rigidbody != null )
63
+ {
64
+ m_Rigidbody . MovePosition ( transform . position + ( m_MoveTowardsPosition * Time . fixedDeltaTime ) ) ;
65
+ }
66
+ }
67
+ }
68
+
69
+ /// <summary>
70
+ /// Handles server notification to client that we need to change direction
71
+ /// </summary>
72
+ /// <param name="direction"></param>
73
+ [ ClientRpc ]
74
+ private void ChangeDirectionClientRpc ( Vector3 direction )
75
+ {
76
+ m_Direction = direction ;
31
77
}
32
78
33
79
private void OnCollisionStay ( Collision collision )
34
80
{
35
- if ( collision . gameObject . CompareTag ( "Floor" ) || collision . gameObject . CompareTag ( "GenericObject" ) )
81
+ if ( IsServer )
36
82
{
37
- return ;
83
+ if ( collision . gameObject . CompareTag ( "Floor" ) || collision . gameObject . CompareTag ( "GenericObject" ) )
84
+ {
85
+ return ;
86
+ }
87
+ Vector3 collisionPoint = collision . collider . ClosestPoint ( transform . position ) ;
88
+ bool moveRight = collisionPoint . x < transform . position . x ;
89
+ bool moveDown = collisionPoint . z > transform . position . z ;
90
+
91
+ ChangeDirection ( moveRight , moveDown ) ;
92
+
93
+ // If we are not the owner then we need to notify the client that their direction
94
+ // must change
95
+ if ( ! IsOwner )
96
+ {
97
+ m_MoveTowardsPosition = m_Direction * m_MoveTowardsPosition . magnitude ;
98
+ ChangeDirectionClientRpc ( m_Direction ) ;
99
+ }
38
100
}
39
- Vector3 collisionPoint = collision . collider . ClosestPoint ( transform . position ) ;
40
- bool moveRight = collisionPoint . x < transform . position . x ;
41
- bool moveDown = collisionPoint . z > transform . position . z ;
42
- ChangeDirection ( moveRight , moveDown ) ;
43
101
}
44
102
45
103
private void ChangeDirection ( bool moveRight , bool moveDown )
0 commit comments