Jittery player movement

Options
I coded a simple method to sync player movement in Unity using Photon Pun2.The problem is when the player moves,the other player sees him moving very jittery.I found out its caused by the code i implemented,but i havent found any good soulution to fix it.

Player Code (the movements sync part):
public class PlayerController : MonoBehaviourPun,IPunObservable
{
private Vector3 smoothMove;

void Update()
    {
        if (view.IsMine)
        {
            ProcessInputs(); //Movement controlls
        }
        else
        {
            smoothMovement();
        }
    }

    private void smoothMovement()
    {
        transform.position = Vector3.Lerp(transform.position, smoothMove, Time.deltaTime * 10);
    }

    public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
    {
        if (stream.IsWriting)
        {
            stream.SendNext(transform.position);
            stream.SendNext(Health);
        }
        else if (stream.IsReading)
        {
            smoothMove = (Vector3)stream.ReceiveNext();
            Health = (float)stream.ReceiveNext();
        }
    }
}

Comments

  • Pablo77
    Options
    Use Lerp Like this:
    You can change 0.1f as you wish.
    transform.position = Vector3.Lerp(transform.position, latestPos, 0.1f);
    
  • Moomit
    Options
    Pablo77 wrote: »
    Use Lerp Like this:
    You can change 0.1f as you wish.
    transform.position = Vector3.Lerp(transform.position, latestPos, 0.1f);
    

    So i should create a int or vector for latestPos?
  • Pablo77
    Options
    Moomit wrote: »
    Pablo77 wrote: »
    Use Lerp Like this:
    You can change 0.1f as you wish.
    transform.position = Vector3.Lerp(transform.position, latestPos, 0.1f);
    

    So i should create a int or vector for latestPos?

    latestPos is Recieved Data From Stream. Its Vector3.
  • Moomit
    Moomit
    edited August 2021
    Options
    Pablo77 wrote: »
    Moomit wrote: »
    Pablo77 wrote: »
    Use Lerp Like this:
    You can change 0.1f as you wish.
    transform.position = Vector3.Lerp(transform.position, latestPos, 0.1f);
    

    So i should create a int or vector for latestPos?

    latestPos is Recieved Data From Stream. Its Vector3.

    When i tried to create like a
    private Vector3 latestPos
    
    then create the function
    private void smoothMovement()
        {
          transform.position = Vector3.Lerp(transform.position, latestPos, 0.1f);
        }
    

    and then adding it to the (stream.IsReading)
    like latestPos = (Vector3)stream.ReceiveNext();

    I am kinda confused how the syncing works
  • Pablo77
    Pablo77
    edited August 2021
    Options
    it looks like it needs to work.now what's the error?
  • Moomit
    Options
    Pablo77 wrote: »
    it looks like it needs to work.now what's the error?

    There is no error actually,the problem is that the smooth movement and this segment:
    public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
        {
            if (stream.IsWriting)
            {
                stream.SendNext(transform.position);
                stream.SendNext(Health);
            }
            else if (stream.IsReading)
            {
                smoothMove = (Vector3)stream.ReceiveNext();
                Health = (float)stream.ReceiveNext();
            }
        }
    

    make the other player move very jittery. I tried to find a solution to fix this but had no luck
  • Pablo77
    Pablo77
    edited August 2021
    Options
    Moomit wrote: »
    Pablo77 wrote: »
    it looks like it needs to work.now what's the error?

    There is no error actually,the problem is that the smooth movement and this segment:
    public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
        {
            if (stream.IsWriting)
            {
                stream.SendNext(transform.position);
                stream.SendNext(Health);
            }
            else if (stream.IsReading)
            {
                smoothMove = (Vector3)stream.ReceiveNext();
                Health = (float)stream.ReceiveNext();
            }
        }
    

    make the other player move very jittery. I tried to find a solution to fix this but had no luck

    Honestly photon is little problem for really smooth transform. Maybe you can try Smooth Sync (Paid Asset). But this code works fine on me
  • Dogu
    Options

    I am using Photon Rigidbody View and OnPhotonSerializeView together. The output is really great like i cannot understand the delay or any jittering.

    using Photon.Pun;
    using UnityEngine;
    
    
    public class PlayerMovement : MonoBehaviour,IPunObservable
    {
        private float horizontal;
        private float speed = 8f;
        [SerializeField] float jumpingPower = 16f;
        private bool isFacingRight = true;
    
    
        [SerializeField] private Rigidbody2D rb;
        [SerializeField] private Transform groundCheck;
        [SerializeField] private LayerMask groundLayer;
    
    
        Vector2 _smoothMovementVector;
        PhotonView pView;
    
    
        private void Start()
        {
            pView = GetComponent<PhotonView>();
        }
    
    
        void Update()
        {
    
    
            if (pView.IsMine)
            {
                InputHandling();
                if (Input.GetButtonDown("Jump") && IsGrounded())
                {
                   //rb.velocity = new Vector2(rb.velocity.x, jumpingPower);
                    rb.velocity = Vector2.Lerp(rb.velocity, new Vector2(rb.velocity.x, jumpingPower), 0.1f);
    
    
                }
    
    
                if (Input.GetButtonUp("Jump") && rb.velocity.y > 0f)
                {
                    //rb.velocity = new Vector2(rb.velocity.x, rb.velocity.y * 0.5f);
                    rb.velocity = Vector2.Lerp(rb.velocity, new Vector2(rb.velocity.x, rb.velocity.y * 0.5f), 0.1f);
    
    
                }
                
            }
            else
            {
                //SmoothMovement();
            }
    
    
            //Flip();
        }
    
    
    
    
        void InputHandling()
        {
            horizontal = Input.GetAxisRaw("Horizontal");
    
    
        }
    
    
        private void FixedUpdate()
        {
            if (pView.IsMine)
            {
                 //rb.velocity = new Vector2(horizontal * speed, rb.velocity.y);
                 rb.velocity = Vector2.Lerp(rb.velocity, new Vector2(horizontal * speed, rb.velocity.y), 0.1f);
            }
            else
            {
                SmoothMovement();
            }
    
    
        }
    
    
        void SmoothMovement()
        {
            rb.velocity = Vector2.Lerp(rb.velocity, _smoothMovementVector, 0.1f);
    
    
        }
    
    
        public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
        {
            if (stream.IsWriting)
            {
                stream.SendNext(rb.velocity);
                //stream.SendNext(Health);
            }
            else if (stream.IsReading)
            {
                _smoothMovementVector = (Vector2)stream.ReceiveNext();
                //Health = (float)stream.ReceiveNext();
            }
        }
    
    
        private bool IsGrounded()
        {
            return Physics2D.OverlapCircle(groundCheck.position, 0.2f, groundLayer);
        }
    
    
    
    
        private void Flip()
        {
            if (isFacingRight && horizontal < 0f || !isFacingRight && horizontal > 0f)
            {
                isFacingRight = !isFacingRight;
                Vector3 localScale = transform.localScale;
                localScale.x *= -1f;
                transform.localScale = localScale;
            }
        }
    
    
    
    
    }