I’m trying to create something that when a player stands on it, they will move with it for a 2d top down game (think like a moving platform). I’ve gotten that logic to work as it’s fairly simple, but what I am having issues with is properly communicating that with the other clients.
Whenever say 2 players get on the moving object they will move with it as expected, but any other player will jitter in the direction that the object is moving. To make things worse (since this object in the end is supposed to be a ship that one of the players can fly) the intensity of the jitter increases with speed. I assume the issue comes from how I’m syncing the movement across the server.
Here is a GIF of the issue → Screen capture - 7767531ab77817c0622197c9db8aa343 - Gyazo
In the GIF I have the “ship” moving up at a velocity of 1 by using rigidbody2d.velocity. Below is my code on the player objects.
if (!hasAuthority)
{
//This is not the player object so we need to update stuff to match the server
transform.position = Vector3.SmoothDamp(transform.position, serverPosition, ref serverPositionSmoothVelocity, 0.25f);
}
And here is how I move the player and then sync up with the server.
private void AuthorityMovement()
{
//Any logic for moving should go here.
//We should make sure if we're on a ship then we should be moving with the ship
if (Ship != null)
{
transform.position += Ship.transform.position - shipOldTransform;
shipOldTransform = Ship.transform.position;
}
//Get the movement input from the player.
float horizontalMove = Mathf.Lerp(0, Input.GetAxis("Horizontal") * MoveSpeed, 0.8f);
float verticalMove = Mathf.Lerp(0, Input.GetAxis("Vertical") * MoveSpeed, 0.8f);
//Go through the various possibilities for what the player is trying to do.
if (PlayerPosition == Position.Empty)
{
PlayerRb.velocity = new Vector2(horizontalMove, verticalMove);
}
//Sync our movement up with the server
CmdUpdatePosition(transform.position);
}
[Command]
void CmdUpdatePosition(Vector3 newPosition)
{
serverPosition = newPosition;
}
I’m fairly new to UNET, but I’ve been giving it my best and this has been stumping me for a weekend or more, and any help or suggestions are welcome.