I have a script, wich allows player to grab and throw game objects, but in photon multiplayer it only works for master client. What i am trying to do is to give permission for other players to use this script, when they try to grab objects. I tried to do it similarly to how it is done in photon demo "ChangeOwner", but it does not work.I have an empty game object in the scene, wich has this script attached to it:
using UnityEngine;
public class DemoOwnershipGui : MonoBehaviour
{
public GUISkin Skin;
public bool TransferOwnershipOnRequest = true;
public void OnOwnershipRequest(object[] viewAndPlayer)
{
PhotonView view = viewAndPlayer[0] as PhotonView;
PhotonPlayer requestingPlayer = viewAndPlayer[1] as PhotonPlayer;
Debug.Log("OnOwnershipRequest(): Player " + requestingPlayer + " requests ownership of: " + view + ".");
if (this.TransferOwnershipOnRequest)
{
view.TransferOwnership(requestingPlayer.ID);
}
}
}
And here is the script, attached to player prefab: (i tried using [punRPC] with functions, but probably did something wrong, also i can not give carry() function the carriedObject parameter using RPC)
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
[RequireComponent( typeof( PhotonView ) )]
public class PickupObjectNetwork : Photon.MonoBehaviour {
GameObject mainCamera;
public bool carrying;
GameObject carriedObject;
public float distance;
public float smooth;
public float throwPower = 10f;
private float chargeTime = 0f;
public Slider throwIndicator;
// Use this for initialization
void Start () {
mainCamera = GameObject.FindWithTag("MainCamera");
}
// Update is called once per frame
void Update () {
if(photonView.isMine)
{
if(carrying) {
carry(carriedObject);
checkDrop();
if (Input.GetMouseButtonUp (0)) {
float pushForce = chargeTime * 10;
pushForce = Mathf.Clamp (pushForce, 1, 20);
throwObject(pushForce);
chargeTime = 0;
}
rotateObject();
} else {
pickup();
}
if(Input.GetMouseButton(0)){
chargeTime += Time.deltaTime;
throwIndicator.value = chargeTime * 20;
}
}
}
void rotateObject() {
carriedObject.transform.Rotate(90,90,90);
}
void carry(GameObject o) {
if (carriedObject == null)
pickup();
o.transform.position = Vector3.Lerp (o.transform.position, mainCamera.transform.position + mainCamera.transform.forward * distance, Time.deltaTime * smooth);
o.transform.rotation = Quaternion.identity;
}
void pickup() {
if(Input.GetKeyDown (KeyCode.E)) {
if( this.photonView.ownerId != PhotonNetwork.player.ID )
{
Debug.Log( "Not requesting ownership. Already mine." );
// this.photonView.RequestOwnership();
}
else
this.photonView.RequestOwnership();
int x = Screen.width / 2;
int y = Screen.height / 2;
Ray ray = mainCamera.GetComponent<Camera>().ScreenPointToRay(new Vector3(x,y));
RaycastHit hit;
if(Physics.Raycast(ray, out hit)) {
Pickupable p = hit.collider.GetComponent<Pickupable>();
if(p != null && hit.distance < 5) {
carrying = true;
carriedObject = p.gameObject;
//p.gameObject.rigidbody.isKinematic = true;
p.gameObject.GetComponent<Rigidbody>().useGravity = false;
}
}
}
}
void checkDrop() {
if(Input.GetKeyDown (KeyCode.E)) {
dropObject();
}
}
void dropObject() {
carrying = false;
//carriedObject.gameObject.rigidbody.isKinematic = false;
carriedObject.gameObject.GetComponent<Rigidbody>().useGravity = true;
carriedObject = null;
}
void throwObject(float pushForce){
carrying = false;
if(carriedObject.tag != "Box")
carriedObject.tag = "Projectile";
carriedObject.GetComponent<Rigidbody> ().AddForce(Camera.main.transform.forward * pushForce * 200);
carriedObject.gameObject.GetComponent<Rigidbody> ().useGravity = true;
carriedObject = null;
}
}