Search Unity

Third Party How can an object be controlled by everyone in the room (photon networking)?

Discussion in 'Multiplayer' started by Boy 121, Jun 28, 2015.

  1. Boy 121

    Boy 121

    Joined:
    Jun 28, 2015
    Posts:
    8
    My problem is I want the football to be controlled by everyone on the room (eg every one can kick the ball). But only the player who instantiated the ball can control(kick) it!

    (I am creating a online two player football game in Unity 5 in C#
    There is two players (which mean the max player in the room is 2)
    If the master client creates a room he will instantiate a character and will control it
    Then if the second player joins the Room as Not A Master Client he will instantiate his player and he can control that player (but not the master client).
    When the second player joins the room, I will instantiate the ball and then can start the game!)

    Here is the code:
    1. void OnJoinedRoom()
    2. {
    3. if (PhotonNetwork.isMasterClient)
    4. {
    5. //instantiate my Master Player
    6. MasterPlayer = PhotonNetwork.Instantiate("HeadRight", Vector3.zero, new Quaternion(0f, 180f, 0f, 0f), 0);
    7. ((MonoBehaviour)leftHead.GetComponent("PlayerTwoMover")).enabled = true;
    8. }

    9. if (!PhotonNetwork.isMasterClient)
    10. {
    11. //instantiate my joined Player
    12. JoinedPlayer = PhotonNetwork.Instantiate("HeadRight", new Vector3(16.4f, 0f, 0f), Quaternion.identity, 0);
    13. //when another player joins (that means two player are now in the room, including the master client) we instantiate the football
    14. football = PhotonNetwork.Instantiate("football_1", new Vector3(10.107f, 6.1372f, 0f), Quaternion.identity, 0);
    15. ((MonoBehaviour)RedCube.GetComponent("PlayerTwoMover")).enabled = true;
    16. }
    17. }
     
    Last edited: Jun 29, 2015
  2. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,066
    Actually, controlling an object by more than one player is pretty tricky and the results are highly depending on the individual lag of players and the physics engine per client. PUN does not support this really.

    You can try to get away with a solution for your game though. You know your requirements best, so it might be possible for your case.
    I would skip the whole system of PhotonViews for this, as you don't want it to dictate ownership and your results.
    Check out PhotonNetwork.RaiseEvent(). You can make up an event anyone can send and which relates to the ball in all cases (so you don't need the PhotonView to find out what ball).
     
  3. Boy 121

    Boy 121

    Joined:
    Jun 28, 2015
    Posts:
    8
    Thank you very much tobiass!!! I have been just waiting for a reply for a long time!
    As you mentioned I dont need the photon network. I got that.
    But can you please show the script in unity networking so that everyone can "control" (kick) the ball!
    Thank you for your replies!
     
  4. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,066
    Sorry, I can't really code this for you and don't have a sample. As said, if a solution is OK for your game, depends a lot on your game. In general, combining physics with networking is really, really tough and won't look good in many cases.
    The high-level help is all I can offer at the moment.
     
  5. Boy 121

    Boy 121

    Joined:
    Jun 28, 2015
    Posts:
    8
    Thank you for the reply!
    One last thing, can you say what method I can use to "control" from everywhere in unity!
     
  6. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,066
    PUN does not have such a method. The trick is to use Unity's physics engine to control the ball and Photon to send the actions of the players. The latter is done by PhotonNetwork.RaiseEvent(). You can make up your own kick events but you also have to figure out what happens if 2 players (or more!) kick the ball at about the same time.
    This is very tricky! Like in: I wouldn't attempt this at all, tricky. But at the same time: If this is your big goal and dream, then find a solution and you have something everyone will envy you for.
     
    Psyco92 likes this.
  7. Boy 121

    Boy 121

    Joined:
    Jun 28, 2015
    Posts:
    8
    No I meant what is the method in Unity Networking (not in photon) that can "allow" me to "control" the ball from everywhere?

    Thankx for all your help!
     
  8. Boy 121

    Boy 121

    Joined:
    Jun 28, 2015
    Posts:
    8
    No I meant what is the method in Unity Networking (not in photon) that can "allow" me to "control" the ball from everywhere?

    Thankx for all your help!
     
  9. Chris196

    Chris196

    Joined:
    Apr 16, 2015
    Posts:
    6
    i have the exact same problem :D i have a jetpackfootball game and all works instead of kicking my ball from every player :( the masterclient can do but others cant. U found a solution??
     
    Eozgungor likes this.
  10. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,735
  11. anandkumar16208

    anandkumar16208

    Joined:
    Apr 3, 2021
    Posts:
    1
    hey that actually very easy
    actually i also struggled a lot but found a simple solution

    private void OnCollisionEnter2D(Collision2D collision)
    {
    if (collision.gameObject.tag == "Player")
    {
    this.photonView.TransferOwnership(collision.gameObject.GetComponent<PhotonView>().Owner);
    }
    }

    you can transfer the ownership when hit a player
    and worked fine for me
    i was creating a online air hockey game.
     
    tigobroers and florinel2102 like this.
  12. chet64646

    chet64646

    Joined:
    May 28, 2021
    Posts:
    1


    How did u instantiate the ball by photon network or in unity

    Can u give that code if possible
     
  13. tigobroers

    tigobroers

    Joined:
    Sep 15, 2021
    Posts:
    1
    I am a complete newbie in this, but hey u got to start somewhere don't u..
    I used the following sentence:

    PhotonNetwork.Instantiate("put your prefab here", Vector3(x, y, z), Quaternion.identity);
     
  14. paymanbehnami

    paymanbehnami

    Joined:
    Aug 21, 2018
    Posts:
    5