PUN Syncing is terrible ...

I'm gonna put this short. My syncing is terrible - the other players are gliding around, and stops moving about three seconds after actually stopping. I'm a tad bit new to networking, but here is my player code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player : Photon.MonoBehaviour {
private Animator anim;
private CharacterController controller;
public float speed = 6.0f;
public float turnSpeed = 60.0f;
private Vector3 moveDirection = Vector3.zero;
public float gravity = 20.0f;
private Vector3 selfPos;
public PhotonView photonView;
public GameObject plCam;

// Use this for initialization
void Start () {
anim = gameObject.GetComponentInChildren();
controller = GetComponent();

}

private void Awake () {
if(photonView.isMine) {
plCam.SetActive(true);
} else {
plCam.SetActive(false);
}
}

private void Update () {
if(photonView.isMine)
checkInput();
else
smoothNetMovement();
}


private void checkInput () {
if(Input.GetKey(KeyCode.W)) {
anim.SetFloat("AnimPar", 1);

} else {
anim.SetFloat("AnimPar", 0);
}

if(controller.isGrounded) {
moveDirection = transform.forward * Input.GetAxis("Vertical") * speed;
}

float turn = Input.GetAxis("Horizontal");
transform.Rotate(0, turn * turnSpeed * Time.deltaTime, 0);
controller.Move(moveDirection * Time.deltaTime);
moveDirection.y -= gravity * Time.deltaTime;


}

private void smoothNetMovement () {
transform.position = Vector3.Lerp(transform.position, selfPos, Time.deltaTime);
}

private void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info) {

if(stream.isWriting) {
stream.SendNext(transform.position);
stream.SendNext(transform.rotation);

}
else {
selfPos = (Vector3)stream.ReceiveNext();
}

}
}

Comments

  • OneManArmy
    OneManArmy ✭✭✭
    edited November 2018
    Adjust smoothing.
    public float smoothing = 7f;
    transform.position = Vector3.Lerp(transform.position, selfPos, Time.deltaTime * smoothing);
  • Thanks. I'll try that when I get home. But is that gonna fix the rotation? And if you have the time, could you get me started on getting my animations working for the other players? (They work fine for the local player)
  • public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
    {
    if (stream.isWriting)
    {
    //We own this player: send the others our data
    stream.SendNext(transform.position);
    stream.SendNext(transform.rotation);
    }
    else
    {
    //Network player, receive data
    correctPlayerPos = (Vector3)stream.ReceiveNext();
    correctPlayerRot = (Quaternion)stream.ReceiveNext();
    }
    }

    public float SmoothingDelay = 5;
    private Vector3 correctPlayerPos = Vector3.zero; //We lerp towards this
    private Quaternion correctPlayerRot = Quaternion.identity; //We lerp towards this

    public void Update()
    {
    if (!photonView.isMine)
    {
    //Update remote player (smooth this, this looks good, at the cost of some accuracy)
    transform.position = Vector3.Lerp(transform.position, correctPlayerPos, Time.deltaTime * this.SmoothingDelay);
    transform.rotation = Quaternion.Lerp(transform.rotation, correctPlayerRot, Time.deltaTime * this.SmoothingDelay);
    }
    }
  • This doesn't quite work. My player spawns in the air, and basically just stand still. This is my current script:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class Player : Photon.MonoBehaviour {
    private Animator anim;
    private CharacterController controller;
    public float speed = 6.0f;
    public float turnSpeed = 60.0f;
    private Vector3 moveDirection = Vector3.zero;
    public float gravity = 20.0f;
    private Vector3 selfPos;
    public PhotonView photonView;
    public GameObject plCam;

    // Use this for initialization
    void Start () {
    anim = gameObject.GetComponentInChildren();
    controller = GetComponent();

    }

    private void Awake () {
    if(photonView.isMine) {
    plCam.SetActive(true);
    } else {
    plCam.SetActive(false);
    }
    }

    public void Update()
    {
    if (!photonView.isMine)
    {
    //Update remote player (smooth this, this looks good, at the cost of some accuracy)
    transform.position = Vector3.Lerp(transform.position, correctPlayerPos, Time.deltaTime * this.SmoothingDelay);
    transform.rotation = Quaternion.Lerp(transform.rotation, correctPlayerRot, Time.deltaTime * this.SmoothingDelay);
    }

    }


    private void checkInput () {
    if(Input.GetKey(KeyCode.W)) {
    anim.SetFloat("AnimPar", 1);

    } else {
    anim.SetFloat("AnimPar", 0);
    }

    if(controller.isGrounded) {
    moveDirection = transform.forward * Input.GetAxis("Vertical") * speed;
    }

    float turn = Input.GetAxis("Horizontal");
    transform.Rotate(0, turn * turnSpeed * Time.deltaTime, 0);
    controller.Move(moveDirection * Time.deltaTime);
    moveDirection.y -= gravity * Time.deltaTime;


    }

    private void smoothNetMovement () {
    transform.position = Vector3.Lerp(transform.position, selfPos, Time.deltaTime);
    }

    public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
    {
    if (stream.isWriting)
    {
    //We own this player: send the others our data
    stream.SendNext(transform.position);
    stream.SendNext(transform.rotation);
    }
    else
    {
    //Network player, receive data
    correctPlayerPos = (Vector3)stream.ReceiveNext();
    correctPlayerRot = (Quaternion)stream.ReceiveNext();
    }
    }

    public float SmoothingDelay = 5;
    private Vector3 correctPlayerPos = Vector3.zero; //We lerp towards this
    private Quaternion correctPlayerRot = Quaternion.identity; //We lerp towards this
    }
  • OneManArmy
    OneManArmy ✭✭✭
    edited November 2018
    
    public class Player : Photon.MonoBehaviour {
    private Animator anim;
    private CharacterController controller;
    public float speed = 6.0f;
    public float turnSpeed = 60.0f;
    private Vector3 moveDirection = Vector3.zero;
    public float gravity = 20.0f;
    public PhotonView photonView;
    public GameObject plCam;
    
    
    public float SmoothingDelay = 7f;
    private Vector3 correctPlayerPos = Vector3.zero; //We lerp towards this
    private Quaternion correctPlayerRot = Quaternion.identity; //We lerp towards this
    
    private void Awake () {
    if(photonView.isMine) {
    plCam.SetActive(true);
    } else {
    plCam.SetActive(false);
    }
    }
    
    
    void Start () {
    anim = gameObject.GetComponentInChildren<Animator>();
    controller = gameObject.GetComponent<CharacterController>();
    }
    
    
    
    public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
    {
    if (stream.isWriting)
    {
    //We own this player: send the others our data
    stream.SendNext(transform.position);
    stream.SendNext(transform.rotation);
    }
    else
    {
    //Network player, receive data
    correctPlayerPos = (Vector3)stream.ReceiveNext();
    correctPlayerRot = (Quaternion)stream.ReceiveNext();
    }
    }
    
    public void Update()
    {
    if (!photonView.isMine)
    {
    //Update remote player (smooth this, this looks good, at the cost of some accuracy)
    transform.position = Vector3.Lerp(transform.position, correctPlayerPos, Time.deltaTime * this.SmoothingDelay);
    transform.rotation = Quaternion.Lerp(transform.rotation, correctPlayerRot, Time.deltaTime * this.SmoothingDelay);
    }
    else
    {
    //your local input here
    if(Input.GetKey(KeyCode.W)) {
    anim.SetFloat("AnimPar", 1);
    
    } else {
    anim.SetFloat("AnimPar", 0);
    }
    
    if(controller.isGrounded) {
    moveDirection = transform.forward * Input.GetAxis("Vertical") * speed;
    }
    
    float turn = Input.GetAxis("Horizontal");
    transform.Rotate(0, turn * turnSpeed * Time.deltaTime, 0);
    controller.Move(moveDirection * Time.deltaTime);
    moveDirection.y -= gravity * Time.deltaTime;
    }	
    } 
    
  • The multiplayer works great - thank you so much! The animations however does not work.
  • nothing will sync "automagically". If you want to play animations on remote clients - send some data.
  • Thank you -- you've saved my day.