Mirror Networking Moving platform jittering
Hello! I have a problem with rigidbody2d player and rigidbody2d platform moving. When there's only one player on platform, it looks perfect but if there's two players at one platform, then the second client (not host) sees how a platform and a player jitters and host only sees how second client is jittering. I went through a bunch of options but couldn't solve this problem so I need help.
OnTriggerEnter2D and OnTriggerExit2D handle player's activeShip variable which is used in PlayerMovement script to sum up ship velocity and player velocity
ShipMovement script snippet:
[SyncVar] public Vector2 velocity;
// When player steps on a platform
[Server]
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.TryGetComponent(out PlayerMovement player))
{
player.activeShip = this;
player.TargetSetActiveShip(gameObject);
}
}
// When player leaves a platform
[Server]
private void OnTriggerExit2D(Collider2D collision)
{
if (collision.TryGetComponent(out PlayerMovement player))
{
if (player.activeShip == this)
{
player.activeShip = null;
player.TargetSetActiveShip(null);
}
}
}
private void FixedUpdate()
{
MovePlatform(); // Server moves a platform
}
[Server]
private void MovePlatform()
{
Rigidbody.velocity = velocity = direction * speed;
}
PlayerMovement script snippet:
public ShipMovementNew activeShip;
[Client]
private void FixedUpdate()
{
if (!hasAuthority) return;
GetInput();
Move();
}
[TargetRpc]
public void TargetSetActiveShip(GameObject ship)
{
if (ship != null)
activeShip = ship.GetComponent<ShipMovementNew>();
else
activeShip = null;
}
private void GetInput()
{
float horizontal = Input.GetAxisRaw("Horizontal");
float vertical = Input.GetAxisRaw("Vertical");
direction = new Vector2(horizontal, vertical).normalized;
}
private void Move()
{
Vector2 velocity = direction * speed;
if (activeShip != null)
velocity += activeShip.velocity;
Rigidbody.velocity = velocity;
}
Ship GameObject
Player GameObject
Your answer
Follow this Question
Related Questions
How can we make 1v1 game with photon network 1 Answer
how to split player objects over network? 0 Answers
Network Manager HUD Component Not Showing 2 Answers
PhotonMono spawns instead of player. 0 Answers