Advertisement
Guest User

Untitled

a guest
Sep 29th, 2011
8,711
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.96 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class cubeMoveAuthoritative : MonoBehaviour {
  5.  
  6.     public NetworkPlayer theOwner;
  7.     float lastClientHInput = 0f;
  8.     float lastClientVInput = 0f;
  9.     float serverCurrentHInput = 0f;
  10.     float serverCurrentVInput = 0f;
  11.  
  12.     void Awake()
  13.     {
  14.         if (Network.isClient)
  15.             enabled = false;
  16.     }
  17.  
  18.     [RPC]
  19.     void SetPlayer(NetworkPlayer player)
  20.     {
  21.         theOwner = player;
  22.         if (player == Network.player)
  23.             enabled = true;
  24.     }
  25.  
  26.     void Update()
  27.     {
  28.         if (theOwner != null && Network.player == theOwner)
  29.         {
  30.             float HInput = Input.GetAxis("Horizontal");
  31.             float VInput = Input.GetAxis("Vertical");
  32.             if (lastClientHInput != HInput || lastClientVInput != VInput)
  33.             {
  34.                 lastClientHInput = HInput;
  35.                 lastClientVInput = VInput;
  36.             }
  37.             if (Network.isServer)
  38.             {
  39.                 SendMovementInput(HInput, VInput);
  40.             }
  41.             else if (Network.isClient)
  42.             {
  43.                 networkView.RPC("SendMovementInput", RPCMode.Server, HInput, VInput);
  44.             }
  45.         }
  46.         if(Network.isServer)
  47.         {
  48.             Vector3 moveDirection = new Vector3(serverCurrentHInput, 0, serverCurrentVInput);
  49.             float speed = 5;
  50.             transform.Translate(speed * moveDirection * Time.deltaTime);
  51.         }
  52.     }
  53.  
  54.     [RPC]
  55.     void SendMovementInput(float HInput, float VInput)
  56.     {  
  57.         serverCurrentHInput = HInput;
  58.         serverCurrentVInput = VInput;
  59.     }
  60.  
  61.     void OnSerializeNetworkView(BitStream stream, NetworkMessageInfo info)
  62.     {
  63.         if (stream.isWriting)
  64.         {
  65.             Vector3 pos = transform.position;      
  66.             stream.Serialize(ref pos);
  67.         }
  68.         else
  69.         {
  70.             Vector3 posReceive = Vector3.zero;
  71.             stream.Serialize(ref posReceive);
  72.             transform.position = posReceive;
  73.         }
  74.     }
  75. }
  76.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement