Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/jackmawer/JacksWorld. An abandoned mini-project that I created while learning to use Unity with networking.
Something went wrong. Try again.
1.5 kB · 44 lines
C#
at master
1234567891011121314151617181920212223242526272829303132333435363738394041424344using UnityEngine;using System.Collections;
public class SmoothSyncMovement : Photon.MonoBehaviour{ public float SmoothingDelay = 5; public void Awake() { if (this.photonView == null || this.photonView.observed != this) { Debug.LogWarning(this + " is not observed by this object's photonView! OnPhotonSerializeView() in this class won't be used."); } }
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(); } }
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); } }
}