Tying to parent objects in game with Photon.PUN OnPhotonSerializeView

The whole answer can be found below.

Please note: The Photon forum is closed permanently. After many dedicated years of service we have made the decision to retire our forum and switch to read-only: we've saved the best to last! And we offer you support through these channels:

Try Our
Documentation

Please check if you can find an answer in our extensive documentation on PUN.

Join Us
on Discord

Meet and talk to our staff and the entire Photon-Community via Discord.

Read More on
Stack Overflow

Find more information on Stack Overflow (for Circle members only).

Write Us
an E-Mail

Feel free to send your question directly to our developers.

Tying to parent objects in game with Photon.PUN OnPhotonSerializeView

Real_Person
2021-10-18 10:43:13

Hi,

I'm very new to photon and have been trying to use it to implement a multiplayer version of my game. However, I'm not sure what the best approach is to resolve my bug issue.

Basically, in a scene, there are multiple objects with their own part script each, and when the player collides with one of these objects it makes them a child of the player and in turn follows the player. This works fine in single-player and even on the host computer of a multiplayer setting but it fails to synchronize on the other client. It as you would call, very buggy. So much so that when I load it up everything shakes (all parts in the scene) on the client's build.

In order to simplify it for a design perspective, these parts start in the scene as children of an empty jigsaw parent prefab. This empty does not move and once all parts have been picked up, it will have no children Therefore it might be of little relevance. (Experimented giving it a photon view as well)

Once a part is placed it will never move again but because they need to be able to move for a period of time, I have given each part a Photon view and photon transform view component.

I have tried synchronizing the transformed parent through OnPhotonSerializeView on each part but after receiving errors, I have found this not to be accepted. I am trying to be mindful to keep sending to the server to a minimum but I don't really know how to do this.

    public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)    
    {    
        //throw new NotImplementedException();    
        if (stream.IsWriting)    
        {    
            //stream.SendNext(transform.parent); does not work    
            stream.SendNext(transform.localPosition);    
        }    
        else if (stream.IsReading)    
        {    
            //transform.parent = (Transform)stream.ReceiveNext(); does not work    
            transform.position = (Vector3)stream.ReceiveNext();    
        }    
    }    
```    

Is it even possible to parent an object through the server?

am I better trying to do a different method for my multiplayer version?

Any help would be much appreciated, thankyou you.

Comments

Tobias
2021-10-18 12:13:39

Well, yes, reparenting objects is actually relatively tricky. The object stops moving by itself and just needs to follow some other object, which is potentially a remote one (and incoming updates may have some jitter / loss, etc).

I think the key words here would be "item" "pickup". If the parts are not distinct objects anymore, I would just destroy the original.

If you are starting with Photon and the game is not half-done yet, it's probably best to skip PUN 2 and solve the problem with Fusion. It's our upcoming networking solution and very advanced, in comparison to PUN 2. There is a "Coming From PUN 2" doc, the Quickstart and the Fusion 100 docs to get you started.

Real_Person
2021-10-18 12:23:05

Tobias 2021-10-18T12:13:39+00:00

Well, yes, reparenting objects is actually relatively tricky. The object stops moving by itself and just needs to follow some other object, which is potentially a remote one (and incoming updates may have some jitter / loss, etc).

I think the key words here would be "item" "pickup". If the parts are not distinct objects anymore, I would just destroy the original.

If you are starting with Photon and the game is not half-done yet, it's probably best to skip PUN 2 and solve the problem with Fusion. It's our upcoming networking solution and very advanced, in comparison to PUN 2. There is a "Coming From PUN 2" doc, the Quickstart and the Fusion 100 docs to get you started.

Where would I start to solve the issue using Fusion in unity? I don't want to destroy the parts because I want them to be visible to all clients when they are placed. Their position only stops being updated and relevant when they are placed by one of the players.

Tobias
2021-10-18 13:56:07

The most common case is that you pick up item and attach them somehow to your character. As in you pick up a hat. In many such cases, you can disable the pickup (in the scene) and just add a "i am wearing hat xyz" into to the character. That means you no longer treat the picked up part as something unique.

However, this may not be what you are after. So it depends.

I noticed you joined the Fusion channel on our Discord, so the discussion is actually going on in parallel.

Let us know what you settled on in this thread, if you find the time. Thanks.

Real_Person
2021-10-22 08:01:48

Somewhat now that I have moved to Fusion have more questions. Thankyou :)

A_k_Fire
2022-08-08 15:29:58

Hii, is there any way to parent a GameObject to Player in game??

A_k_Fire
2022-08-08 15:32:18

I have tried PhotonNetwrok.Instantiate() it do instantition all over player but when i try to get this object i am having problem to link GameObject to player ????

A_k_Fire
2022-08-08 16:27:21

hii there is my written script but i stll get error in it.

Back to top