- Home /
How to request access for actions in PUN multiplayer?
Hi, i have a script, wich allows player to grab and throw game objects, but in photon multiplayer it onl works for master client. What i`m trying to do is to give permission to other players to use this script, when they try to grab objects, but curretly it doesn`t work. I tried to do it similary to how it is done in photon demo. 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 players:
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;
}
}
What happens now: when non-master player tries to grab object by pressing E it (object) "presses" into the ground, but for the master it works fine. Maybe you`ve had same issues before
Your answer
Follow this Question
Related Questions
How to make script work for all players Unity Photon? 1 Answer
Instantiating scene objects in Photon Multiplayer. 0 Answers
Unity Collision Doesn't Work when Two Players Instantiated 1 Answer
Why do other players have stuttering / laggy movement in Photon? 0 Answers
This is only activating the hat locally or an all clients or no clients. 1 Answer