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 · Nov 21, 2016 at 07:19 AM · c#unity5photon

How to make script work for all players Unity Photon?

Prelude: Again i`m asking about moving my stupid stones and boxes, but after almost 2 months of work, i am still unable to work this out. (I`ve been learning unity for 3 months total).

I want players to be able to move and throw gameObjects in the scene using this script:

 [RequireComponent( typeof( PhotonView ) )]
 public class PickupObjectNetwork : Photon.MonoBehaviour {
 public GameObject mainCamera;    
 public bool carrying;
 GameObject carriedObject;
 public float distance;
 public float smooth;
 public float throwPower = 10f;
 private float chargeTime = 0f;    
 // Use this for initialization
 void Start () {
     mainCamera = GameObject.FindWithTag("MainCamera");            
 }

 // Update is called once per frame
 void Update () {
     if(photonView.isMine)
     {            
         if(carriedObject == null)
         {
             carrying = false;
         }
         if(gameObject == null)
             return;
         
         if(carrying) {
             photonView.RPC("carry", PhotonTargets.AllBuffered);
             photonView.RPC("checkDrop", PhotonTargets.AllBuffered);
 
             if (Input.GetMouseButtonUp (0)) {
                 float pushForce = chargeTime * 10;
                 pushForce = Mathf.Clamp (pushForce, 1, 20);
                 photonView.RPC("throwObject", PhotonTargets.AllBuffered, pushForce);
                 chargeTime = 0;
             }                
         } else {
             if(Input.GetKeyDown (KeyCode.E)) {                    
                 photonView.RPC("pickup", PhotonTargets.AllBuffered);
             }
         }

         if(Input.GetMouseButton(0)){
             chargeTime += Time.deltaTime;
         }
     }
 }

 [PunRPC]
 void carry() {            
     if (carriedObject == null)
     {
         return;
     }        
     Debug.Log("picked up object with id: " + carriedObject.GetComponent<PhotonView>().viewID);
     carriedObject.transform.position = Vector3.Lerp (carriedObject.transform.position, mainCamera.transform.position + mainCamera.transform.forward * distance, Time.deltaTime * smooth);
     carriedObject.transform.rotation = Quaternion.identity;
 }

 [PunRPC]
 void pickup() {                
         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.GetComponent<Rigidbody>().velocity = Vector3.zero;
                 p.gameObject.GetComponent<Rigidbody>().useGravity = false;                            
             }
         }        
 }
 [PunRPC]
 void checkDrop() {            
     if(Input.GetKeyDown (KeyCode.E)) {
         dropObject();
     }
 }
 [PunRPC]
 void dropObject() {        
     carrying = false;        
     carriedObject.gameObject.GetComponent<Rigidbody>().useGravity = true;
     carriedObject = null;
 }
 [PunRPC]
 void throwObject(float pushForce){            
     carrying = false;
     if(carriedObject.tag != "BoxMultiplayer")
         carriedObject.tag = "Projectile";
     carriedObject.GetComponent<Rigidbody> ().AddForce(Camera.main.transform.forward * pushForce * 200);
     carriedObject.gameObject.GetComponent<Rigidbody> ().useGravity = true;            
     carriedObject = null;
 }    
 }

I instantiate scene objects using PhotonNetwork.InstantiateSceneObject(); This script works only for master client, non-master clients can`t move objects. I also tried this script (randomly):

http://pastebin.com/7DgjZ9U7 (had to put it in pastebin, because it hadn`t been displayed correctly for some reason)

I don`t really know how to work with AllocateViewId(). When i use this script, both players can move sceneObjects, but object movement is not synchronized (object positions are not updating for other players). Also, when one of the players destroy gameObject, by throwing it into the wall, which destroys new collisions with PhotonNetwork.Destroy(), this player freezes in place for other players.

I know i`m asking a lot of questions, but i don`t see other variants how to finish my project

Comment
Add comment · Show 2
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
avatar image GiViZed · Nov 19, 2016 at 05:07 PM 0
Share

also i don`t know how to avoid calling RPC`s in the Update function in this script, i know that this is bad

avatar image Gascoine_17 · Feb 24, 2017 at 08:01 AM 0
Share

Did you figure this out? Because it's my problem too.

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Unity Collision Doesn't Work when Two Players Instantiated 1 Answer

Instantiating scene objects in Photon Multiplayer. 0 Answers

Why do other players have stuttering / laggy movement in Photon? 0 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers


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