Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by GiViZed · Oct 13, 2016 at 03:40 PM · c#unity5photon

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges