Advertisement
mihaiavram96

Untitled

Oct 24th, 2017
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.69 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. [RequireComponent(typeof (PhotonView) )]
  4. public class NetworkProp : Photon.MonoBehaviour {
  5.  
  6.     private Vector3 correctPlayerPos = Vector3.zero; // We lerp towards this
  7.     private Quaternion correctPlayerRot = Quaternion.identity; // We lerp towards this
  8.     private bool correctGravity = true;
  9.    
  10.    
  11.  
  12.     // Use this for initialization
  13.     void Start () {
  14.             Debug.Log("The starting position of the: " + gameObject.name + " is: " + transform.position + ". Client id: " + photonView.ownerId);
  15.             Debug.Log("The world position of the: " + gameObject.name + " is: " + transform.TransformPoint(transform.position) + ". Client id: " + photonView.ownerId);
  16.        
  17.     }
  18.    
  19.     // Update is called once per frame
  20.     void Update () {
  21.         if (!photonView.isMine && photonView.owner != null)
  22.             {
  23.                 // If photonView.owner == null it means that the scene owns the object and it causes the Lerp to happen
  24.                 // before the first player joins and takes ownership of that object.
  25.                 transform.position = Vector3.Lerp(transform.position, this.correctPlayerPos, Time.deltaTime * 5);
  26.                 transform.rotation = Quaternion.Lerp(transform.rotation, this.correctPlayerRot, Time.deltaTime * 5);
  27.                 gameObject.GetComponent<Rigidbody>().useGravity = correctGravity;
  28.             }
  29.  
  30.             if (!photonView.isMine && PropSelected())
  31.             {
  32.                 ChangeOwner();
  33.             }
  34.        
  35.     }
  36.  
  37.     void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info){      
  38.             if (stream.isWriting){
  39.             // We own this player: send the others our data
  40.             stream.SendNext(transform.position);
  41.             stream.SendNext(transform.rotation);
  42.             stream.SendNext(gameObject.GetComponent<Rigidbody>().useGravity);
  43.             }
  44.             else{
  45.             // Network player, receive data
  46.             this.correctPlayerPos = (Vector3)stream.ReceiveNext();
  47.             this.correctPlayerRot = (Quaternion)stream.ReceiveNext();
  48.             this.correctGravity = (bool)stream.ReceiveNext();
  49.             }      
  50.     }
  51.  
  52.     public void ChangeOwner()
  53.     {
  54.        
  55.         if (this.photonView.ownerId == PhotonNetwork.player.ID)
  56.         {
  57.             Debug.Log("Not requesting ownership. Already mine.");
  58.             return;
  59.         }
  60.         Debug.Log("Attempt to Request Ownership");
  61.         this.photonView.RequestOwnership();
  62.        
  63.     }
  64.  
  65.     private bool PropSelected()
  66.     {
  67.         return VRController.IsPointingAtObject()
  68.             && VRController.CurrentGameObject().tag == "Prop"
  69.             && VRController.AppButtonUp();
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement