I am having a HELL of a time getting this to work!
I have COMBED through the two scripts and even resorted to copying over the prefab!
my scripts are in C# though, so i am afraid i may have ported it wrong!
SO far, the server moves both cubes and both get updated on the client, and the client can move his but it doesnt get updated on the server! is there a problem with my RPC calls? the debugger says it is recieving and relaying the RPC call.
Here is the spawn script. If this ends up working after some help then … here is the C# port of tutorial 3! =)
using UnityEngine;
using System.Collections;
public class SpawnScript : MonoBehaviour
{
public Transform playerPrefab;
public ArrayList playerScripts = new ArrayList();
void OnServerInitialized()
{
SpawnPlayer(Network.player);
}
void OnPlayerConnected(NetworkPlayer newPlayer)
{
SpawnPlayer(newPlayer);
}
void SpawnPlayer(NetworkPlayer toSpawn)
{
int playerNum = int.Parse(toSpawn.ToString());
Debug.Log("Creating Player with number: " + playerNum);
Transform newTrans = (Transform)Network.Instantiate(playerPrefab, transform.position, transform.rotation, playerNum);
NetworkView newObjectsNetworkView = newTrans.networkView;
playerScripts.Add(newTrans.GetComponent("PlayerScript"));
newObjectsNetworkView.RPC("SetPlayer", RPCMode.AllBuffered, toSpawn);
}
void OnPlayerDisconnected(NetworkPlayer player)
{
foreach (PlayerScript script in playerScripts)
{
if (player == script.owner)
{
Network.RemoveRPCs(script.gameObject.networkView.viewID);
Network.Destroy(script.gameObject);
playerScripts.Remove(script);
}
}
int playerNumber = int.Parse(player.ToString());
Network.RemoveRPCs(Network.player, playerNumber);
Network.RemoveRPCs(player);
Network.DestroyPlayerObjects(player);
}
void OnDisconnectedFromServer(NetworkDisconnection info)
{
Application.LoadLevel("NetworkMenu");
}
}
and the player script
using UnityEngine;
using System.Collections;
public class PlayerScript : MonoBehaviour
{
public NetworkPlayer owner;
//Last input value, we're saving this to save network messages/bandwidth.
private float lastClientHInput = 0;
private float lastClientVInput = 0;
//The input values the server will execute on this object
private float serverCurrentHInput = 0;
private float serverCurrentVInput = 0;
void Awake()
{
if (Network.isClient)
{
this.enabled = false;
}
}
[RPC]
void SetPlayer(NetworkPlayer player)
{
this.owner = player;
if (player == Network.player)
{
this.enabled = true;
}
}
void Update()
{
//Debug.Log("Update: Owner:" + owner.ToString());
// client code
if (owner!=null Network.player==owner)
{
float hInput = Input.GetAxis("Horizontal");
float vInput = Input.GetAxis("Vertical");
if (true)
{
lastClientHInput = hInput;
lastClientVInput = vInput;
if (Network.isServer)
{
Debug.Log("Update: isServer: Owner Send Movement Input" + owner.ToString());
SendMovementInput(hInput, vInput);
}
else
{
Debug.Log("Update: isClient: Owner Send Movement Input" + owner.ToString());
SendMovementInput(hInput, vInput);
networkView.RPC("SendMovementInput", RPCMode.Server, hInput, vInput);
}
}
}
if(Network.isServer || Network.player == owner){
Debug.Log("Update: Network: isServer: Owner Set" + owner.ToString());
//Only the owner can move the cube!
Vector3 moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
float speed = 5;
transform.Translate(speed * moveDirection * Time.deltaTime);
////Save some network bandwidth; only send an rpc when the position has moved more than X
//if (Vector3.Distance(transform.position, lastPosition) >= 0.05)
//{
// lastPosition = transform.position;
// //Send the position Vector3 over to the others; in this case all clients
// networkView.RPC("SetPosition", RPCMode.Others, transform.position);
//}
}
}
[RPC]
void SendMovementInput(float hInput, float vInput)
{
//Debug.Log("Send Movement Input: Owner" + owner.ToString());
// SendMovementInput(hInput, vInput);
serverCurrentHInput = hInput;
serverCurrentVInput = vInput;
}
void OnSerializeNetworkView(BitStream stream, NetworkMessageInfo info)
{
// send position
if (stream.isWriting)
{
Vector3 pos = transform.position;
stream.Serialize(ref pos);
}
// receive position
else
{
Vector3 posRec = Vector3.zero;
stream.Serialize(ref posRec);
transform.position = posRec;
}
}
}