how to sync GameObject variable in photon network? - unity3d

I am making Network game using Unity. I want to configure my network using PUN2. In particular, I notice that I can synchronize several basic variables through the OnPhotonSerializeView () method. However, this does not include custom classes such as GameObject and Transform. So what should I do to synchronize the Target Transform in that inspector view?

There are a few ways to tackle this. It depends on what you need this target for, and if it makes since for the object to exist on the other clients if it's not being targeted by any object.
Suppose you have Player that is synced, and a blue ball and a red ball in the scene.
The player has a ball that they are "holding". You want to trigger a synchronization so on all clients, if the player is holding a red ball it will hold it for all clients, and if they hold the blue ball it will do so for all clients.
The problem linking the blue ball in one client to all the blue balls in everyone else's client.
Option a) Give each ball a name and have some static dictionary mapping their name to the object in the scene (you can make them add themselves on the Awake or Start of the ball). Now you can synchronize the name of the ball that a player holds (a string in oppose to an object). When a client gets the name change, you can handle the name change as looking it up in the static dictionary, and setting it as the ball being held.
Option b) Synchronize the player AND the balls. Anything logic-related will only happen for the current player. So if the local player holds a ball, the player position will be synced and the ball position will be synced but nothing else will happen for the other players. This of course assumes this works for whatever you are trying to accomplish.
Option c) Serialize the ball. Send the data which describes the ball (size, color, etc) and create a new instance on the client side. This will NOT be tied to any existing balls on the client side. You may need check and destroy whichever ball is already being held. This is probably the least effective way to accomplish this, performance-wise.

Related

Client side prediction with a Shared object(Player - Ball relationship)

Our team is developing a football game. But we are stuck in a step.
We can't move forward because of this.
When we make client side prediction, the local player plays on the current time.
But the other objects like balls and other players play in the past(due to latency).
Normally for other objects we use interpolation but this doesn't work for the ball because local player interact with it while moving and it causes the other players to intertwine with the ball.
We tried to take the ball to the current state simulating physics and run all work-flow on the local. But this time when other players interact with the ball, they seems like moving the ball from far behind. Because of the other players in the past.
What is the best method to use for this situation?

Unity best practice: How to represent (hold and pass) the game items/enemies/attacks/etc? as scriptable-objects or prefabs?

I want to create a game with tons of different enemies, items, accessories, heroes, attacks and more.
To make it easily expandable and maintenance, I want to create the game in the most modular way possible so nothing in the game will be built-in and every aspect of the game will be assembled and configured.
But I'm undecided as to how the elements should be modeled:
As ScriptableObjects - (for example) every enemy will be configured and represented by some scriptabe-object and this asset will contains the enemy stats and behaviours but also the enemy prefabs so the script will tell us how to create the enemy object in the game.
so if I have some place that I want to put some enemy in it, I should (for example) attach on the inspector the enemy's scriptable-object.
As Prefabs - every enemy will be represented by some prefab and the prefab root object will contains some scriptable-object that its purpose is to hold some specific enemy stats and behaviours and the enemy will be act according to them.
so if I have some place that I want to put some enemy in it, I should attach on the inspector the enemy's prefab.
Is there some best practice for that? a way that will make my life easier when the game will be particularly large and complex?

Resonance Audio Source - Unity3D Collision

I added the Reasonance Audio Source to an object and wanted to generate a sound when a collision between that object and another object occurred.
The issue I am having is that OnCollisionEnter in Unity is triggered whenever the object gets within the "audio region" (a blue disk attached to the object with a certain radius). However, what I am interested is when the physical collision occurs, and not this "audio" collision.
One way to go around it is just to create a copy of that object, without the Reasonance Audio Source which follows the motion of the other object. Then you trigger collisions the object copy. But I was wondering if there is any way to differentiate these two types of collisions in Unity?
Here is an example. Red ball is the object I want to generate the sound from and that has the Reasonance Audio Source as a component. The blue region is created from that component. Whenever the blue disk touches the grey floor, a collision is generated. I would like to disable this "audio" collision and have only a collision being detected when a physical collision between the red ball and the grey floor occurs.
EDIT: Found the problem. I had been testing the PhysicsManager properties (Edit/Project Settings/Physics) and I had the "Default contact offset" set to 0.01 which by change was the same as the blue region, so I went looking for a problem in the wrong place. I now changed the contact offset to a smaller value and all is ok.
In the PhysicsManager properties (Edit/Project Settings/Physics) the "Default contact offset" defines the blue region. Change the value to a smaller one to resize the contact region.
OLD ANSWER:
Without knowing your scene or code i can only speculate that you might want to turn of the audio sources collision and trigger it manually when the collision with the object occurs. Just attach a script that listens for a collision and then trigger the collision event of the audio source (or just play the sound).

Physics don't work as intended using firebase realtime database for unity

I am making a 1v1 multiplayer game such as 8 ball pool using unity. The overview of the approach I am using currently is:
1) Sync positions of each ball on both players.
2) If it's player1's turn: Get vector3 force applied on white ball and run the physics on
3) Send force from player1 on white ball and run it on player2.
4) Change turn once all balls are static or pocketed.
Problem is that even though: White ball's initial position and force applied are same for both players. The end positions of all balls are not the same. There is randomness in the physics I guess. The balls move and behave differently especially after collisions. There is sort of a butterfly effect and the end result of positions of balls on table is different for both players.
My question is that how will you approach in making a 1v1 8 ball pool where there are lot of objects and physics going around and ensure that everything happens the same way for both players.
Physics need to happen on a single instance of your app then propagated to others from there. Otherwise said, one player should be server + client and other players should be clients. All physics happen on server and clients send their commands to it like force applied on white ball, direction, etc.
Unity HLAPI for networking : https://docs.unity3d.com/Manual/UNetUsingHLAPI.html

UNITY: Synchronizing variables across a network

Ok, so I'm trying to implement networking for a school project. (Part of my team's final project)
It's supposed to operate on a turn-based system. IE, when a player is done, they hit "next turn" and they lose control, while the other player gains control.
So, to test my networking and get the basics going, I've made a little program that spawns a cube whenever a server is joined or created (so one cube for each player), and each player is able to move only their own cube.
So far, I can have the cubes moving around and their transforms are perfectly synced up on both instances of the game simultaneously, and each player can only control their own cube.
However, when I click "Next turn", the turn only changes on ONE of the instances.
According to this, it SHOULD be watching the turn change:
And this is my turncontrol code (It's a little sloppy for now until I find a hardcore way to set the code's player number depending on whether they're the host or client)
How the heck do I make sure that when I click "Next Turn", the turn changes in both instances, and how do I make sure that the cube's player number is dependent on whether they're the host or client?
That and I have the very strong suspicion that the assignment of a player's "player number" (IE, whether they're player 1 or 2) is being assigned locally (Not reflected across instances) as well, since each player can only move when the turn is set to 1 (Meaning that when both players spawn, they're both set to be player 1. :s)
Any assistance would be appreciated. Networking is completely new to me and I'm wigging out trying to wrap my head around it.