Jittery player movement
The whole answer can be found below.
Try Our
Documentation
Please check if you can find an answer in our extensive documentation on PUN.
Join Us
on Discord
Meet and talk to our staff and the entire Photon-Community via Discord.
Read More on
Stack Overflow
Find more information on Stack Overflow (for Circle members only).
Jittery player movement
Moomit
2021-08-06 16:57:19
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
Use Lerp Like this:
You can change 0.1f as you wish.
transform.position = Vector3.Lerp(transform.position, latestPos, 0.1f);
@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?
@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.
@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
it looks like it needs to work.now what's the error?
@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
@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
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;
}
}
}
Back to top