How to do a head rotation script over the network?
I'm using Photon for networking and I'm trying to serialize the head rotation of players for my Oculus game. The rotation works locally but this in combination will not show the other character rotating their head over the network. I cannot add the OVRCameraRig for coordinates of the player across the network because that would conflict with the local player's CameraRig. Does anyone have any experience with this? Basically every oculus game with networking has it. I assume a large number of people have it figured out. Here's my head rotation script.
Here's my networking script (I just started the networking on this a few days ago).
using UnityEngine;
using System.Collections;
public class HeadScript : MonoBehaviour {
public GameObject charHead;
private Quaternion startCharHeadPosition;
private Vector3 correctionEuler;
protected OVRCameraRig riftController = null;
// Implement limits at
// z = 85 degrees
// y = 120 degrees
// x = 85 degrees
void Start () {
riftController = this.GetComponentInChildren<OVRCameraRig>();
startCharHeadPosition = charHead.transform.localRotation;
//charHead.transform.localScale = new Vector3(0.1f, 0.1f, 0.1f);
}
void LateUpdate () {
Quaternion currentTorso = transform.rotation;
Vector3 eulerHead = charHead.transform.rotation.eulerAngles;
Vector3 eulerRift = riftController.centerEyeAnchor.transform.rotation.eulerAngles;
Vector3 eulerRiftHeadMap = new Vector3 (-eulerRift.z, eulerRift.x, -eulerRift.y);
Quaternion newHead = Quaternion.Euler(eulerRiftHeadMap);
charHead.transform.localRotation = (newHead * startCharHeadPosition);
}
}
Here's my networking script (I just started the networking on this a few days ago).
using UnityEngine;
using System.Collections;
public class NetworkCharacter : Photon.MonoBehaviour {
Vector3 realPosition = Vector3.zero;
Quaternion realRotation = Quaternion.identity;
Vector3 realVelocity = Vector3.zero;
Quaternion headRotation;
Quaternion initialHead;
public Transform neckJoint;
Animator anim;
HeadScript headScript;
bool gotFirstUpdate = false;
void Start ()
{
anim = GetComponent<Animator>();
if(anim == null) {
Debug.LogError ("You forgot to put an Animator component on this character prefab");
}
headScript = GetComponent<HeadScript>();
if(headScript == null) {
Debug.LogError ("You forgot to put an HeadScript component on this character prefab");
}
if(neckJoint == null) {
neckJoint = transform.Find("Joint_4_4");
}
initialHead = neckJoint.localRotation;
}
// Update is called once per frame
void Update () {
if(photonView.isMine)
{
//Do nothing -- the character input is moving us
} else {
transform.position = Vector3.Lerp(transform.position, realPosition, 0.1f);
transform.rotation = Quaternion.Lerp(transform.rotation, realRotation, 0.1f);
rigidbody.velocity = Vector3.Lerp(rigidbody.velocity, realVelocity, 0.1f);
neckJoint.transform.localRotation = Quaternion.Lerp(neckJoint.transform.localRotation, realRotation, 0.1f);
//Debug.LogError(neckJoint.transform.localRotation);
}
}
void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
{
if(stream.isWriting)
{
// This is OUR player. We need to send our actual position to the network.
stream.SendNext(transform.position);
stream.SendNext(transform.rotation);
stream.SendNext(rigidbody.velocity);
stream.SendNext(anim.GetFloat("Speed"));
stream.SendNext(anim.GetBool("Jumping"));
stream.SendNext(rigidbody.freezeRotation);
//stream.SendNext(headScript.charHead.transform.localRotation);
stream.SendNext(neckJoint.transform.localRotation);
} else {
// This is someone else's player. We need to receive their position as of a few
// milliseconds ago, and update our version of that player.
realPosition = (Vector3)stream.ReceiveNext();
realRotation = (Quaternion)stream.ReceiveNext();
realVelocity = (Vector3)stream.ReceiveNext();
anim.SetFloat("Speed", (float)stream.ReceiveNext());
anim.SetBool("Jumping", (bool)stream.ReceiveNext());
rigidbody.freezeRotation = (bool)stream.ReceiveNext();
//headScript.charHead.transform.localRotation = (Quaternion)stream.ReceiveNext();
headRotation = (Quaternion)stream.ReceiveNext();
if(gotFirstUpdate == false)
{
transform.position = realPosition;
transform.rotation = realRotation;
gotFirstUpdate = true;
}
}
}
}
