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 /
  • Help Room /
This question was closed May 07, 2017 at 02:00 AM by OfficialCoatsee for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by OfficialCoatsee · May 03, 2017 at 02:08 AM · c#scripting problemnetworkingphotonrpc

[Photon Networking] Passing My Player scripts 'Target' object to the Uncontrolled version of My Player

Helllooooo!

I am quite further a long in my project now, just trying to setup some multiplayer. I have pretty much everything working, that I need so far... Except... well, the targeting!

If I play on my computer, and my mobile at the same time, it works awesome. My players are both there, and they are in real time, and they are moving how they should.

The problem is with the NPC's in the game.

If I for instance, select an NPC in my game, and then choose to attack it - all from my mobile.

My player on both screens, will go to the target, however only one will start attacking the NPC.

The movement is being tracked, but not the scripts variables - which the targets are GameObjects in this case.

Now I did some testing with other variables, and things like Strings, Booleans and Integers... Even Vectors work. But GameObjects seem not to. So at the moment, I have specific GUI and everything popping up exactly how it should and health bars and levels remain the same across all clients. But NOT FOR TARGETING TARGETS! D:

I have tried a few methods, one which seems to sometimes work, so I don't think it's down the correct avenue of code.

Is there any way to send GameObjects through a network AS a variable, so that the uncontrolled viewer of my player on the client of another person, can then be fed the correct -updated - information.

Any help or links are appreciated, I've been searching for 2 days and have only got so far.

Cheers,

Justin!

Comment
Add comment · Show 7
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 OfficialCoatsee · May 03, 2017 at 02:54 AM 0
Share

Any suggestions? + bump

avatar image OfficialCoatsee · May 03, 2017 at 03:27 AM 0
Share

$$anonymous$$ay have figured out an answer to my own question, but help is still appreciated. Will keep you updated!

avatar image OfficialCoatsee · May 03, 2017 at 06:38 AM 0
Share

Nope. That wasn't it. Anyone have any bright ideas? Come on.

avatar image ChristianSimon · May 03, 2017 at 08:14 AM 0
Share

Hi,

$$anonymous$$y player on both screens, will go to the target, however only one will start attacking the NPC.

This sounds like you forgot a photonView.is$$anonymous$$ine condition somewhere.

avatar image OfficialCoatsee ChristianSimon · May 03, 2017 at 08:35 AM 0
Share

Perhaps, perhaps. But that seems a little too obvious.

This is my current code.

 void Update () {
         if (photonView.is$$anonymous$$ine) {
             //do nothing...
         } else {
             if (target_object != null) {
                 GetComponent<Player> ().target_object = target_object;
             }
             GetComponent<Player> ().username = realUsername;
             GetComponent<Player> ().current_health = currentHealth;
             GetComponent<Player> ().isDead = isDead;
             GetComponent<Player> ().is_player_being_attacked = is_player_being_attacked;
             transform.position = Vector3.Lerp (transform.position, realPosition, 0.1f);
             transform.rotation = Quaternion.Lerp (transform.rotation, realRotation, 0.1f);
         }
     }
 
     void OnPhotonSerializeView(PhotonStream stream, Photon$$anonymous$$essageInfo info) {
 
         if (stream.isWriting) {
             //this is YOU.
 
             //Debug.Log(gameObject.GetPhotonView().viewID);
 
             //stream.SendNext(GetComponent<PhotonView>().viewID); //sending my viewer id. I am the sender.
 
             if (GetComponent<Player> ().target_object != null) {
                 if (GetComponent<Player> ().target_object.transform.tag == "tree") {
                     stream.SendNext (GetComponent<Player> ().target_object.GetComponent<TreeAI> ().game_object_id);
                 }
             }
 
             stream.SendNext(GetComponent<Player>().username);
             stream.SendNext (GetComponent<Player> ().current_health);
             stream.SendNext (GetComponent<Player> ().isDead);
             stream.SendNext(GetComponent<Player>().is_player_being_attacked);
             stream.SendNext(transform.position);
             stream.SendNext (transform.rotation);
 
         } else {
             //this is someone else...
 
             //int viewer_id_of_sender = (int)stream.ReceiveNext (); //getting the viewer id from the sender.
             //GameObject senders_character = PhotonView.Find (viewer_id_of_sender).gameObject;
 
             //Debug.Log ("Viewer is: " + PhotonView.Find (viewerId).gameObject);
             //Debug.Log ("Target object is: " + PhotonView.Find (viewerId).gameObject.GetComponent<Player>().target_object);
 
             //target_object = senders_character.GetComponent<Player>().target_object;
 
 
             if (GetComponent<Player>().target_object == null) {
                 int temporary_game_object_id = (int)stream.ReceiveNext ();
                 GameObject gameHandler = GameObject.Find ("GameHandler").gameObject;
                 target_object = gameHandler.GetComponent<GameObjectHandler> ().gameobjects [temporary_game_object_id].gameObject;
                 Debug.Log ("The receiver is now Targetting " + target_object);
             }
 
             realUsername = (string)stream.ReceiveNext ();
             currentHealth = (int)stream.ReceiveNext ();
             isDead = (bool)stream.ReceiveNext();
             is_player_being_attacked = (bool)stream.ReceiveNext();
             realPosition = (Vector3)stream.ReceiveNext ();
             realRotation = (Quaternion)stream.ReceiveNext ();
 
         }
 
     }


well, that's this part of it anyway. It semi works. I need a better way to pass game objects through the script to the same script on another client.

avatar image ChristianSimon OfficialCoatsee · May 04, 2017 at 08:13 AM 0
Share

Do you actually need to send the game object across the network? Which additional information are required except of the few shown in the code snippet, because using OnPhotonSerializeView(...) should be fine in this case. Besides I think you don't need the Update() function to set the received values, you can set them directly when those are being received (except position and rotation when using Lerp function). Another way to send variables across the network is to use RPCs and also possible in this case.

If you still want to send a game object, you can try registering a custom type in Photon. You can find an example here.

Show more comments

1 Reply

  • Sort: 
avatar image
1
Best Answer

Answer by OfficialCoatsee · May 07, 2017 at 01:58 AM

So, I figured it out. The code I used is as follows.

Call Method:

 if (target_object.GetComponent<PhotonView> () != null) {
         int target_id = target_object.GetComponent<PhotonView> ().viewID;
         photonView.RPC ("networkSetTargetObject", PhotonTargets.All, target_id);
 }

The above method triggers the RPC displayed below. It sends the viewID of the GameObject in question.

 [PunRPC]
 void networkSetTargetObject(int view_id) {
 if (view_id != null) {
     if (photonView != null) {
         GameObject testSubject = PhotonView.Find (view_id).gameObject;
             target_object = testSubject;
         }
     }
 }

You don't need all the if statements in the networkSetTargetObject() function, I don't think. But they have prevented a lot of errors from coming up during runtime.

What we are doing - is getting the gameObject that the passed over the network viewID is assigned to.

And that's it, this works, all done.

Comment
Add comment · Share
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

Follow this Question

Answers Answers and Comments

377 People are following this question.

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

Related Questions

Photon Networking - How to move all players from game scene to current room scene? 0 Answers

How to choose different teams one by one 1 Answer

How to sync RPC function Photon Network? 1 Answer

ClientRpc/Commands between two seperate projects? 0 Answers

uNet. Spawning objects over the network. 0 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