Syncing Player Movement in 2D
Im Making a 2D space shooter game and im trying to sync player movement and rotation over the network but i cant seem to do it smoothly. and help would be appreciated.
PlayerMovement
void Update () {
if (Input.GetKey (KeyCode.A)) {
transform.Translate (Vector2.down * speed);
}
if (Input.GetKey (KeyCode.D)) {
transform.Translate (Vector2.up * speed);
}
Rotation Script
void Update ()
{
Vector2 mouse = Camera.main.ScreenToViewportPoint (Input.mousePosition);
Vector3 objpos = Camera.main.WorldToViewportPoint (transform.position);
Vector2 relobjpos = new Vector2 (objpos.x - 0.5f, objpos.y - 0.5f);
Vector2 relmousepos = new Vector2 (mouse.x - 0.5f, mouse.y - 0.5f) - relobjpos;
float angle = Vector2.Angle (Vector2.up, relmousepos);
if (relmousepos.x > 0)
angle = 360 - angle;
Quaternion quat = Quaternion.identity;
quat.eulerAngles = new Vector3 (0, 0, angle);
transform.rotation = quat;
}
}
Answer by romatallinn · Oct 29, 2016 at 02:16 PM
To smooth movement over network, you have to use interpolation. I believe every tutorial about movement explains it. So, try to look at this tutor or google "network interpolation unity"
Also, one thing more that doesn't relate to network. I see you use transform.Translate . I found this to be quite important to use Time.deltaTime every time when you Translate or Rotate. It makes movement independent on frame rate. You can google "independent frame rate movement unity" to get more information about it.
So, you should use this instead:
transform.Translate (Vector2.down * speed * Time.deltaTime);
I tried it and it works but its still very jolty ive messed around with all the settings and it doesnt change anything