Here is my problem: I got a chat network that works fine (all objects and scripts are already on scene). However, I cannot sync player positions (spheres, for the time being).
I spawn players like in the following snipet. There is an object in the scene that has the script
The spherePrefab has a NetworkView with State Sync off, and Observed set to None.
It also has a script with the following code
if (Mine) {
if (Input.GetMouseButton (0)) {
RaycastHit hit;
if (Physics.Raycast (cam.ScreenPointToRay (Input.mousePosition), out hit, 100.0F)) {
_moveTo = hit.point;
}
}
Vector3 direction = _moveTo - _sphere.transform.position;
direction = direction.normalized;
Vector3 nextMovementStep = transform.position;
nextMovementStep.x += direction.x * Time.deltaTime * speed;
nextMovementStep.z += direction.z * Time.deltaTime * speed;
transform.position = nextMovementStep;
// this method never runs
networkView.RPC ("SetPosition", RPCMode.Others, transform.position);
}
}
[RPC]
public void SetPosition (Vector3 newPosition)
{
Debug.Log ("im in");
transform.position = newPosition;
}
However, the SetPosition method is never executed. I am testing this while playing in two instances of the webplayer, in my browser.
The chat I implemented works like a charm, but this doesn’t. I have read the documentation and searched online and cannot, for the life of me, figure out why it won’t work. Each player can only control his own sphere. Other players are spawned, but just stand still. Also, there are no errors in the console.
appels, I see now that I might not have answered your question. I generated my view id’s myself so that I can sync each instance of a player.
So say:
1 - player1 logs in, allocates a view ID for himself, and buffers an RPC
2 - player2 logs in, allocates a view ID for himself, and buffers an RPC
3 - player2 receives the RPC from player1, which contains the instantiate and view ID, so player2 spawn an instante of player1, and sets it view ID to be synced with player1
4- player1 receives the RPC from player 2, (…)
I think my rationality is right here, but it just does’t work.
I tried it now with Network.Instantiate. Same result ._.
As soon as this gets called on one of the clients, the prefab should appear in all scenes connected to that server instance.
If this doesn’t work, you need to debug why it doesn’t work.
Turn on network debugging, then at least you can see if the buffered RPC’s reach the server and clients.
Don’t complicate your code unless you need to.
I was doing this as a tutorial, because I will be needing to sync events, not positions.
And right now I am not able to do it.
I debugged, the chat messages work fine (the object is already in the scene).
However, as far as the server is aware, the RPC’s for the SetPosition never get called. And the line that calls it gets executed… this is mind-boggling.
I need to set the Observed property of NetworkView in the prefab to the script that will be handling the RPCs.
It is weird that I have to do it on instantiated objects, but not on objects that are already in the scene…
Anyways, thank you appels, you helped me to retain my sanity
EDIT:
If I instantiate the stuff manually it still doesn’t work ._.
Ok now I really got it.
The problem was that I didn’t have the RPC implemented in the server.
Can’t I just use the server to relay RPCs? Because Putting RPCs methods in the server that do nothing just so it works is tedious and ( to my eyes) completely unnecessary.