Patasuss

Untitled

Mar 2nd, 2014
390
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.75 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class RigWalker : MonoBehaviour {
  5.     public float speed = 5f;
  6.     public float jumpMul = 4f;
  7.     bool grounded = false;
  8.    
  9.     GameObject Cam;
  10.    
  11.     //Network Variables
  12.     Vector3 objPos;
  13.     Quaternion objRot;
  14.     Vector3 rigVel;
  15.     Vector3 angVel;
  16.     Vector3 CamForward;
  17.     Vector3 CamRight;
  18.     float AxisVertical = 0f;
  19.     float AxisHorizontal = 0f;
  20.    
  21.     // Use this for initialization
  22.     void Start () {
  23.         if(Network.isClient && networkView.isMine)
  24.         {
  25.             Cam = GameObject.Find("MainCamera");
  26.             Cam.transform.parent = gameObject.transform;
  27.         }
  28.     }
  29.    
  30.     void OnCollisionStay()
  31.     {
  32.         grounded = true;   
  33.     }
  34.    
  35.     // Update is called once per frame
  36.     void FixedUpdate () {
  37.         if(Network.isServer)
  38.         {
  39.             Vector3 walkVec = CamForward * AxisVertical + CamRight * AxisHorizontal;
  40.             walkVec = new Vector3(walkVec.x, 0, walkVec.z) * speed;
  41.            
  42.             Vector3 velocity = rigidbody.velocity;
  43.             Vector3 velChange = (walkVec - velocity);
  44.             velChange.y = 0;
  45.            
  46.             rigidbody.AddForce(velChange, ForceMode.VelocityChange);
  47.            
  48.             if(grounded && Input.GetButton("Jump"))
  49.                 rigidbody.AddForce(new Vector3(0, jumpMul * 10, 0));
  50.         }
  51.        
  52.         grounded = false;
  53.     }
  54.    
  55.     void OnSerializeNetworkView(BitStream stream, NetworkMessageInfo info)
  56.     {
  57.         if(stream.isWriting) //Sending information
  58.         {
  59.             if(Network.isServer) //Server -> Client
  60.             {
  61.                 objPos = rigidbody.transform.position;
  62.                 objRot = rigidbody.transform.rotation;
  63.                 rigVel = rigidbody.velocity;
  64.                 angVel = rigidbody.angularVelocity;
  65.                
  66.                 stream.Serialize(ref objPos);
  67.                 stream.Serialize(ref objRot);
  68.                 stream.Serialize(ref rigVel);
  69.                 stream.Serialize(ref angVel);
  70.             }
  71.             else if(Network.isClient) //Client -> Server
  72.             {
  73.                 if(networkView.isMine) //Me -> Server
  74.                 {
  75.                     AxisVertical = Input.GetAxis("Vertical");
  76.                     AxisHorizontal = Input.GetAxis("Horizontal");
  77.                     CamForward = Cam.transform.forward;
  78.                     CamRight = Cam.transform.right;
  79.                    
  80.                     stream.Serialize(ref AxisVertical);
  81.                     stream.Serialize(ref AxisHorizontal);
  82.                     stream.Serialize(ref CamForward);
  83.                     stream.Serialize(ref CamRight);
  84.                 }
  85.             }
  86.         }
  87.         else if(stream.isReading) //Receiving information
  88.         {
  89.             if(Network.isServer) //Client -> Server
  90.             {
  91.                 stream.Serialize(ref AxisVertical);
  92.                 stream.Serialize(ref AxisHorizontal);
  93.                 stream.Serialize(ref CamForward);
  94.                 stream.Serialize(ref CamRight);
  95.             }
  96.             else if(Network.isClient) //Server -> Client
  97.             {
  98.                 stream.Serialize(ref objPos);
  99.                 stream.Serialize(ref objRot);
  100.                 stream.Serialize(ref rigVel);
  101.                 stream.Serialize(ref angVel);
  102.                
  103.                 rigidbody.MovePosition(objPos);
  104.                 rigidbody.MoveRotation(objRot);
  105.                 rigidbody.velocity = rigVel;
  106.                 rigidbody.angularVelocity = angVel;
  107.             }
  108.         }
  109.     }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment