Help with smoothing players

Hi,

I am trying to understand how to smooth out how other players see each other.

I have the following script and I am writing the details in that I believe I need to do to make it smoother but I cannot figure out what to do in the receive part.

[code2=csharp]using UnityEngine;
using System.Collections;

public class SimpleControl : Photon.MonoBehaviour {

public float speed = 10f;

// Update is called once per frame
void Update ()
{
if (Input.GetKey(KeyCode.A))
this.transform.position += Vector3.left * (speed * Time.deltaTime);

if (Input.GetKey(KeyCode.D))
this.transform.position += Vector3.right * (speed * Time.deltaTime);

if (Input.GetKey(KeyCode.W))
this.transform.position += Vector3.forward * (speed * Time.deltaTime);

if (Input.GetKey(KeyCode.S))
this.transform.position += Vector3.back * (speed * 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);
stream.SendNext(Vector3.left);
stream.SendNext(Vector3.right);
stream.SendNext(Vector3.forward);
stream.SendNext(Vector3.back);
}
else
{
// Network player, receive data
this.transform.position = (Vector3) stream.ReceiveNext();
this.transform.rotation = (Quaternion) stream.ReceiveNext();

}
}
}[/code2]

Can anyone help please?

Thanks
Doomie22

Comments

  • Instead of posting code that doesn't work how you like, post what you think is the way to smooth out the movement.
    Or take a look at the different scripts that the Synchronization Demo includes in the PUN package.
  • Thank you for your post. i have looked at the marco tutorial and a few on youtube. I believe that this should of smoothed it out but its exactly the same.
    public class SimpleControl : Photon.MonoBehaviour {


    public float speed = 10f;
    private Vector3 correctPlayerPos;
    private Quaternion correctPlayerRot;


    // Update is called once per frame
    void Update ()
    {
    if (Input.GetKey(KeyCode.A))
    this.transform.position += Vector3.left * (speed * Time.deltaTime);

    if (Input.GetKey(KeyCode.D))
    this.transform.position += Vector3.right * (speed * Time.deltaTime);

    if (Input.GetKey(KeyCode.W))
    this.transform.position += Vector3.forward * (speed * Time.deltaTime);

    if (Input.GetKey(KeyCode.S))
    this.transform.position += Vector3.back * (speed * Time.deltaTime);

    if (!photonView.isMine)
    {
    transform.position = Vector3.Slerp(transform.position, this.correctPlayerPos, Time.deltaTime * 5);
    transform.rotation = Quaternion.Slerp(transform.rotation, this.correctPlayerRot, Time.deltaTime * 5);
    }
    }

    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
    this.correctPlayerPos = (Vector3)stream.ReceiveNext();
    this.correctPlayerRot = (Quaternion)stream.ReceiveNext();
    //this.transform.position = (Vector3) stream.ReceiveNext();
    //this.transform.rotation = (Quaternion) stream.ReceiveNext();


    }
    }
    }
  • It looks ok.
    Maybe deltaTime*5 is not the best value in your case. You could try different values. This will depend on the latency.

    Also keep in mind: This is a very simple way to smooth movement. It tries to move you to some target position bit by bit. But whenever a new update comes in, it simply drops the older target and goes for the new one. As that's further away, it will speed up your movement temporarily. If no update comes in (cause it might be delayed), then the char will slow down and this might look bad, too.

    With more effort, you could use more than one target positions and get a smoother movement. You might also track when each update was created and sent. If you take that into account, too, then movement gets even smoother.

    I think one of the Synchronization Demo boxes is moving with slightly more elaborate smoothing.
    All in all, we don't have a super smooth sample at the moment, so you might have to look around some more.