Advertisement
Guest User

Untitled

a guest
Aug 8th, 2014
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.07 KB | None | 0 0
  1. /// <summary>
  2. /// Player network controller.
  3. /// David Oneill
  4. /// This class manages all the movement and animation syncing (Mecanim) over the Photon network
  5. /// 25/6/2014
  6. /// </summary>
  7. using UnityEngine;
  8. using System.Collections;
  9.  
  10. public class PlayerNetworkController : Photon.MonoBehaviour{
  11.  
  12.     public Transform player;
  13.     public Transform lookAtTarget;
  14.     public GameObject activeWeaponGO;//communications empty go for passing strings
  15.     [HideInInspector]
  16.     public GameObject currentlyActiveWeapon = null;
  17.     public Transform weaponMount;// dummys mount obj//
  18.     private string prevWeapon = "";
  19.     [HideInInspector]
  20.     public string currentActiveWeapon = ""; //need to set this from weapon manager, so it can set the name of the empthy go for syncing
  21.     //string currentWeaponName = "";
  22.     private Vector3 correctPlayerPos = Vector3.zero; // We lerp towards this position
  23.     private Quaternion correctPlayerRot = Quaternion.identity; // We lerp towards this rotataion
  24.     private Vector3 correctLookPos = Vector3.zero; // We lerp towards this position
  25.     private Quaternion correctLookRot = Quaternion.identity; // We lerp towards this rotataion
  26.  
  27.     float smoothTime = 0.1f;
  28.  
  29.     private Animator anim;
  30.  
  31.     // Use this for initialization
  32.     void Awake () {
  33.         //currentlyActiveWeapon = null;
  34.         anim = player.GetComponent<Animator>();
  35.         if(anim == null) {
  36.             Debug.LogError ("No Animator!");
  37.         }
  38.         currentlyActiveWeapon = null;
  39.     }
  40.  
  41.     // Update is called once per frame
  42.     void Update () {
  43.         if (!photonView.isMine)
  44.         {
  45.             player.position = Vector3.Lerp(player.position, this.correctPlayerPos,  smoothTime);
  46.             player.rotation = Quaternion.Lerp(player.rotation, this.correctPlayerRot,  smoothTime);
  47.  
  48.             lookAtTarget.position = Vector3.Lerp(lookAtTarget.position, this.correctLookPos,  smoothTime);
  49.             lookAtTarget.rotation = correctLookRot;
  50.  
  51.             activeWeaponGO.name = currentActiveWeapon;
  52.  
  53.             if(prevWeapon != currentActiveWeapon){
  54.                 for(int c = 0; c < weaponMount.childCount; c++){
  55.                     if(weaponMount.GetChild(c).name != currentActiveWeapon){
  56.                         weaponMount.GetChild(c).gameObject.SetActive(false);
  57.                     }
  58.                     else{
  59.                         weaponMount.GetChild(c).gameObject.SetActive(true);
  60.                         currentlyActiveWeapon = weaponMount.GetChild(c).gameObject;
  61.                     }
  62.                 }
  63.                 prevWeapon = currentActiveWeapon;
  64.             }
  65.  
  66.         }
  67.         activeWeaponGO.name = currentActiveWeapon;
  68.  
  69.         if(anim == null)
  70.         {
  71.             Debug.Log ("Freak Out");
  72.  
  73.         }
  74.     }
  75.  
  76.     void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info){
  77.         if(stream.isWriting)//then this is our player.
  78.         {
  79.             stream.SendNext(player.position);
  80.             stream.SendNext(player.rotation);
  81.             stream.SendNext(lookAtTarget.position);
  82.             stream.SendNext(lookAtTarget.rotation);
  83.             stream.SendNext(activeWeaponGO.name);
  84.  
  85.  
  86.             stream.SendNext(anim.GetFloat("Direction"));
  87.             stream.SendNext(anim.GetFloat("Speed"));
  88.             stream.SendNext(anim.GetFloat("Vvelocity"));
  89.             stream.SendNext(anim.GetBool("Grounded"));
  90.             stream.SendNext(anim.GetBool("Crouch"));
  91.             stream.SendNext(anim.GetBool("Sprint"));
  92.             stream.SendNext(anim.GetInteger("WeaponInt"));
  93.             stream.SendNext(anim.GetBool("Aim"));
  94.             stream.SendNext(anim.GetBool("Reloading"));
  95.  
  96.             //Debug.Log ("I am streaming Stuff!");
  97.  
  98.         }
  99.         else // its someone elses character!
  100.         {
  101.             correctPlayerPos = (Vector3)stream.ReceiveNext();
  102.             correctPlayerRot = (Quaternion)stream.ReceiveNext();
  103.             correctLookPos = (Vector3)stream.ReceiveNext();
  104.             correctLookRot = (Quaternion)stream.ReceiveNext();
  105.             currentActiveWeapon = (string)stream.ReceiveNext();
  106.            
  107.             anim.SetFloat("Direction", (float)stream.ReceiveNext());
  108.             anim.SetFloat("Speed", (float)stream.ReceiveNext());
  109.             anim.SetFloat("Vvelocity", (float)stream.ReceiveNext());
  110.             anim.SetBool("Grounded", (bool)stream.ReceiveNext());
  111.             anim.SetBool("Crouch", (bool)stream.ReceiveNext());
  112.             anim.SetBool("Sprint", (bool)stream.ReceiveNext());
  113.             anim.SetInteger("WeaponInt", (int)stream.ReceiveNext());
  114.             anim.SetBool("Aim", (bool)stream.ReceiveNext());
  115.             anim.SetBool("Reloading", (bool)stream.ReceiveNext());
  116.  
  117.  
  118.             //Debug.Log ("I am Recieving Stuff!");
  119.         }
  120.     }
  121.  
  122.  
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement