Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public override void OnNetworkSpawn()
m_Rigidbody = GetComponent<Rigidbody>();
if (m_Rigidbody != null)
{
m_Rigidbody.isKinematic = !NetworkObject.IsOwner;
m_Rigidbody.isKinematic = !NetworkObject.NetworkManager.IsServer;
}
}

Expand All @@ -44,18 +44,10 @@ private void Update()
m_RandomMovement.enabled = !m_RandomMovement.enabled;
}
}
}

private void FixedUpdate()
{
if (m_NetworkedObject && m_NetworkedObject.NetworkManager && m_NetworkedObject.NetworkManager.IsListening)
{

if (!m_NetworkedObject.IsOwner)
{
return;
}
else if (m_RandomMovement.enabled)
if (m_RandomMovement.enabled)
{
m_RandomMovement.Move(MoveSpeed);
}
Expand Down
76 changes: 67 additions & 9 deletions testproject/Assets/Tests/Manual/Scripts/RandomMovement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,87 @@ public override void OnNetworkSpawn()
m_Rigidbody = GetComponent<Rigidbody>();
if (NetworkObject != null && m_Rigidbody != null)
{
m_Rigidbody.isKinematic = !NetworkObject.IsOwner;
if (!m_Rigidbody.isKinematic)
if (NetworkObject.IsOwner)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IsServer here too?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, that is just to initialize the direction randomly.

{
ChangeDirection(true, true);
}
}
}

/// <summary>
/// Notify the server of any client side change in direction or speed
/// </summary>
/// <param name="moveTowards"></param>
[ServerRpc(RequireOwnership = false)]
private void MovePlayerServerRpc(Vector3 moveTowards)
{
m_MoveTowardsPosition = moveTowards;
}

private Vector3 m_MoveTowardsPosition;

public void Move(int speed)
{
m_Rigidbody.MovePosition(transform.position + m_Direction * (speed * Time.fixedDeltaTime));
// Server sets this locally
if (IsServer && IsOwner)
{
m_MoveTowardsPosition = (m_Direction * speed);
}
else if (!IsServer && IsOwner)
{
// Client must sent Rpc
MovePlayerServerRpc(m_Direction * speed);
}
}

// We just apply our current direction with magnitude to our current position during fixed update
private void FixedUpdate()
{
if (IsServer && NetworkObject && NetworkObject.NetworkManager && NetworkObject.NetworkManager.IsListening)
{
if (m_Rigidbody == null)
{
m_Rigidbody = GetComponent<Rigidbody>();
}
if (m_Rigidbody != null)
{
m_Rigidbody.MovePosition(transform.position + (m_MoveTowardsPosition * Time.fixedDeltaTime));
}
}
}

/// <summary>
/// Handles server notification to client that we need to change direction
/// </summary>
/// <param name="direction"></param>
[ClientRpc]
private void ChangeDirectionClientRpc(Vector3 direction)
{
m_Direction = direction;
}

private void OnCollisionStay(Collision collision)
{
if (collision.gameObject.CompareTag("Floor") || collision.gameObject.CompareTag("GenericObject"))
if (IsServer)
{
return;
if (collision.gameObject.CompareTag("Floor") || collision.gameObject.CompareTag("GenericObject"))
{
return;
}
Vector3 collisionPoint = collision.collider.ClosestPoint(transform.position);
bool moveRight = collisionPoint.x < transform.position.x;
bool moveDown = collisionPoint.z > transform.position.z;

ChangeDirection(moveRight, moveDown);

// If we are not the owner then we need to notify the client that their direction
// must change
if (!IsOwner)
{
m_MoveTowardsPosition = m_Direction * m_MoveTowardsPosition.magnitude;
ChangeDirectionClientRpc(m_Direction);
}
}
Vector3 collisionPoint = collision.collider.ClosestPoint(transform.position);
bool moveRight = collisionPoint.x < transform.position.x;
bool moveDown = collisionPoint.z > transform.position.z;
ChangeDirection(moveRight, moveDown);
}

private void ChangeDirection(bool moveRight, bool moveDown)
Expand Down