Skip to content

Instantly share code, notes, and snippets.

@haydenjameslee
Last active October 1, 2020 07:27
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save haydenjameslee/499483f79620bb36607d to your computer and use it in GitHub Desktop.
Save haydenjameslee/499483f79620bb36607d to your computer and use it in GitHub Desktop.
using UnityEngine;
using System.Collections;
// For use with Photon and SteamVR
public class NetworkedPlayer : Photon.MonoBehaviour
{
public GameObject avatar;
public Transform playerGlobal;
public Transform playerLocal;
void Start ()
{
Debug.Log("Player instantiated");
if (photonView.isMine)
{
Debug.Log("Player is mine");
playerGlobal = GameObject.Find("[CameraRig]").transform;
playerLocal = playerGlobal.Find("Camera (head)");
this.transform.SetParent(playerLocal);
this.transform.localPosition = Vector3.zero;
}
}
void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
{
if (stream.isWriting)
{
stream.SendNext(playerGlobal.position);
stream.SendNext(playerGlobal.rotation);
stream.SendNext(playerLocal.localPosition);
stream.SendNext(playerLocal.localRotation);
}
else
{
this.transform.position = (Vector3)stream.ReceiveNext();
this.transform.rotation = (Quaternion)stream.ReceiveNext();
avatar.transform.localPosition = (Vector3)stream.ReceiveNext();
avatar.transform.localRotation = (Quaternion)stream.ReceiveNext();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment