Skip to content

Commit 19e6d3c

Browse files
fix: player movement (#1063)
* fix Due to recent permissions changes and how near future permissions will impact movement, the following fixes issue with player movement and handling the wall collision changing direction. * refactor and style Just refactored a bit and added some comments in a few places. Co-authored-by: M. Fatih MAR <mfatihmar@gmail.com>
1 parent d30f617 commit 19e6d3c

File tree

2 files changed

+69
-19
lines changed

2 files changed

+69
-19
lines changed

testproject/Assets/Tests/Manual/Scripts/PlayerMovementManager.cs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public override void OnNetworkSpawn()
3131
m_Rigidbody = GetComponent<Rigidbody>();
3232
if (m_Rigidbody != null)
3333
{
34-
m_Rigidbody.isKinematic = !NetworkObject.IsOwner;
34+
m_Rigidbody.isKinematic = !NetworkObject.NetworkManager.IsServer;
3535
}
3636
}
3737

@@ -44,18 +44,10 @@ private void Update()
4444
m_RandomMovement.enabled = !m_RandomMovement.enabled;
4545
}
4646
}
47-
}
4847

49-
private void FixedUpdate()
50-
{
5148
if (m_NetworkedObject && m_NetworkedObject.NetworkManager && m_NetworkedObject.NetworkManager.IsListening)
5249
{
53-
54-
if (!m_NetworkedObject.IsOwner)
55-
{
56-
return;
57-
}
58-
else if (m_RandomMovement.enabled)
50+
if (m_RandomMovement.enabled)
5951
{
6052
m_RandomMovement.Move(MoveSpeed);
6153
}

testproject/Assets/Tests/Manual/Scripts/RandomMovement.cs

Lines changed: 67 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,29 +17,87 @@ public override void OnNetworkSpawn()
1717
m_Rigidbody = GetComponent<Rigidbody>();
1818
if (NetworkObject != null && m_Rigidbody != null)
1919
{
20-
m_Rigidbody.isKinematic = !NetworkObject.IsOwner;
21-
if (!m_Rigidbody.isKinematic)
20+
if (NetworkObject.IsOwner)
2221
{
2322
ChangeDirection(true, true);
2423
}
2524
}
2625
}
2726

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+
2839
public void Move(int speed)
2940
{
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;
3177
}
3278

3379
private void OnCollisionStay(Collision collision)
3480
{
35-
if (collision.gameObject.CompareTag("Floor") || collision.gameObject.CompareTag("GenericObject"))
81+
if (IsServer)
3682
{
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+
}
38100
}
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);
43101
}
44102

45103
private void ChangeDirection(bool moveRight, bool moveDown)

0 commit comments

Comments
 (0)