Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /// <summary>
- /// Player network controller.
- /// David Oneill
- /// This class manages all the movement and animation syncing (Mecanim) over the Photon network
- /// 25/6/2014
- /// </summary>
- using UnityEngine;
- using System.Collections;
- public class PlayerNetworkController : Photon.MonoBehaviour{
- public Transform player;
- public Transform lookAtTarget;
- public GameObject activeWeaponGO;//communications empty go for passing strings
- [HideInInspector]
- public GameObject currentlyActiveWeapon = null;
- public Transform weaponMount;// dummys mount obj//
- private string prevWeapon = "";
- [HideInInspector]
- public string currentActiveWeapon = ""; //need to set this from weapon manager, so it can set the name of the empthy go for syncing
- //string currentWeaponName = "";
- private Vector3 correctPlayerPos = Vector3.zero; // We lerp towards this position
- private Quaternion correctPlayerRot = Quaternion.identity; // We lerp towards this rotataion
- private Vector3 correctLookPos = Vector3.zero; // We lerp towards this position
- private Quaternion correctLookRot = Quaternion.identity; // We lerp towards this rotataion
- float smoothTime = 0.1f;
- private Animator anim;
- // Use this for initialization
- void Awake () {
- //currentlyActiveWeapon = null;
- anim = player.GetComponent<Animator>();
- if(anim == null) {
- Debug.LogError ("No Animator!");
- }
- currentlyActiveWeapon = null;
- }
- // Update is called once per frame
- void Update () {
- if (!photonView.isMine)
- {
- player.position = Vector3.Lerp(player.position, this.correctPlayerPos, smoothTime);
- player.rotation = Quaternion.Lerp(player.rotation, this.correctPlayerRot, smoothTime);
- lookAtTarget.position = Vector3.Lerp(lookAtTarget.position, this.correctLookPos, smoothTime);
- lookAtTarget.rotation = correctLookRot;
- activeWeaponGO.name = currentActiveWeapon;
- if(prevWeapon != currentActiveWeapon){
- for(int c = 0; c < weaponMount.childCount; c++){
- if(weaponMount.GetChild(c).name != currentActiveWeapon){
- weaponMount.GetChild(c).gameObject.SetActive(false);
- }
- else{
- weaponMount.GetChild(c).gameObject.SetActive(true);
- currentlyActiveWeapon = weaponMount.GetChild(c).gameObject;
- }
- }
- prevWeapon = currentActiveWeapon;
- }
- }
- activeWeaponGO.name = currentActiveWeapon;
- if(anim == null)
- {
- Debug.Log ("Freak Out");
- }
- }
- void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info){
- if(stream.isWriting)//then this is our player.
- {
- stream.SendNext(player.position);
- stream.SendNext(player.rotation);
- stream.SendNext(lookAtTarget.position);
- stream.SendNext(lookAtTarget.rotation);
- stream.SendNext(activeWeaponGO.name);
- stream.SendNext(anim.GetFloat("Direction"));
- stream.SendNext(anim.GetFloat("Speed"));
- stream.SendNext(anim.GetFloat("Vvelocity"));
- stream.SendNext(anim.GetBool("Grounded"));
- stream.SendNext(anim.GetBool("Crouch"));
- stream.SendNext(anim.GetBool("Sprint"));
- stream.SendNext(anim.GetInteger("WeaponInt"));
- stream.SendNext(anim.GetBool("Aim"));
- stream.SendNext(anim.GetBool("Reloading"));
- //Debug.Log ("I am streaming Stuff!");
- }
- else // its someone elses character!
- {
- correctPlayerPos = (Vector3)stream.ReceiveNext();
- correctPlayerRot = (Quaternion)stream.ReceiveNext();
- correctLookPos = (Vector3)stream.ReceiveNext();
- correctLookRot = (Quaternion)stream.ReceiveNext();
- currentActiveWeapon = (string)stream.ReceiveNext();
- anim.SetFloat("Direction", (float)stream.ReceiveNext());
- anim.SetFloat("Speed", (float)stream.ReceiveNext());
- anim.SetFloat("Vvelocity", (float)stream.ReceiveNext());
- anim.SetBool("Grounded", (bool)stream.ReceiveNext());
- anim.SetBool("Crouch", (bool)stream.ReceiveNext());
- anim.SetBool("Sprint", (bool)stream.ReceiveNext());
- anim.SetInteger("WeaponInt", (int)stream.ReceiveNext());
- anim.SetBool("Aim", (bool)stream.ReceiveNext());
- anim.SetBool("Reloading", (bool)stream.ReceiveNext());
- //Debug.Log ("I am Recieving Stuff!");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement