Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- [RequireComponent(typeof (PhotonView) )]
- public class NetworkProp : Photon.MonoBehaviour {
- private Vector3 correctPlayerPos = Vector3.zero; // We lerp towards this
- private Quaternion correctPlayerRot = Quaternion.identity; // We lerp towards this
- private bool correctGravity = true;
- // Use this for initialization
- void Start () {
- Debug.Log("The starting position of the: " + gameObject.name + " is: " + transform.position + ". Client id: " + photonView.ownerId);
- Debug.Log("The world position of the: " + gameObject.name + " is: " + transform.TransformPoint(transform.position) + ". Client id: " + photonView.ownerId);
- }
- // Update is called once per frame
- void Update () {
- if (!photonView.isMine && photonView.owner != null)
- {
- // If photonView.owner == null it means that the scene owns the object and it causes the Lerp to happen
- // before the first player joins and takes ownership of that object.
- transform.position = Vector3.Lerp(transform.position, this.correctPlayerPos, Time.deltaTime * 5);
- transform.rotation = Quaternion.Lerp(transform.rotation, this.correctPlayerRot, Time.deltaTime * 5);
- gameObject.GetComponent<Rigidbody>().useGravity = correctGravity;
- }
- if (!photonView.isMine && PropSelected())
- {
- ChangeOwner();
- }
- }
- void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info){
- if (stream.isWriting){
- // We own this player: send the others our data
- stream.SendNext(transform.position);
- stream.SendNext(transform.rotation);
- stream.SendNext(gameObject.GetComponent<Rigidbody>().useGravity);
- }
- else{
- // Network player, receive data
- this.correctPlayerPos = (Vector3)stream.ReceiveNext();
- this.correctPlayerRot = (Quaternion)stream.ReceiveNext();
- this.correctGravity = (bool)stream.ReceiveNext();
- }
- }
- public void ChangeOwner()
- {
- if (this.photonView.ownerId == PhotonNetwork.player.ID)
- {
- Debug.Log("Not requesting ownership. Already mine.");
- return;
- }
- Debug.Log("Attempt to Request Ownership");
- this.photonView.RequestOwnership();
- }
- private bool PropSelected()
- {
- return VRController.IsPointingAtObject()
- && VRController.CurrentGameObject().tag == "Prop"
- && VRController.AppButtonUp();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement