1
\$\begingroup\$

i'm having some troubles in my first, simple, multiplayer racing game, using Photon and Unity. Following classic photon tutorial i've written the code below, but, it won't work properly. In particular there are any smooth movement and "other" player car continue to appear and disapper... maybe i could post a video.

void Update() {
if (gsAppSettings.gameType == Enumerators.GameType.MultiPlayer) {
            if (pv.isMine) {    //pv is PhotonView          
                GetPlayerInput ();          
                WeaponsManagement ();   
            } else {
                SyncMovement ();
            }
        }
        else if (gsAppSettings.gameType == Enumerators.GameType.SinglePlayer) {
            GetPlayerInput ();              
            WeaponsManagement ();   
        }
}
void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
    {
        if (stream.isWriting) {
            if (photonView.isMine) {
                stream.SendNext (transform.position);
                stream.SendNext (transform.rotation);
                stream.SendNext (rb.velocity);
            }
        } else {
            correctPlayerPos = (Vector3)stream.ReceiveNext();
            correctPlayerRot = (Quaternion)stream.ReceiveNext();
            currentVelocity = (Vector3)stream.ReceiveNext();
            updateTime = Time.time;
        }
    }

private void SyncMovement() {       
        Vector3 projectedPosition = this.correctPlayerPos + currentVelocity * (Time.time - updateTime);
        transform.position = Vector3.Lerp (transform.position, projectedPosition, Time.deltaTime * 4);
        transform.rotation = Quaternion.Lerp (transform.rotation, this.correctPlayerRot, Time.deltaTime * 4);
    }

EDIT: This is video. As you can see, the low car is "flipping" up and down https://www.youtube.com/watch?v=QKpYl7C5rl0&feature=youtu.be

\$\endgroup\$
  • \$\begingroup\$ Did you make sure to add this script to the photon view update list? What happens if you simply lerp the position to the correctPlayerPos? Also a video of the problem would be useful :) \$\endgroup\$ – Iggy May 13 '16 at 11:25
  • \$\begingroup\$ Here there is the video: youtube.com/watch?v=QKpYl7C5rl0&feature=youtu.be \$\endgroup\$ – stighy May 13 '16 at 18:26
  • \$\begingroup\$ From the video it looks like your projectedPosition is wrong. Again try just setting the position without the fancy lerp / prediction, and see if it helps. \$\endgroup\$ – Iggy May 14 '16 at 9:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.