Advertisement
Guest User

Untitled

a guest
Oct 19th, 2016
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.34 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.Networking;
  3.  
  4. [NetworkSettings(channel = 0, sendInterval = 1 / 30f)]
  5. public class networkPlayer : NetworkBehaviour
  6. {
  7.     [Range(0.0f, 20.0f)]
  8.     public float lerpAmount = 1;
  9.  
  10.     [SyncVar]
  11.     Vector2 syncPosition;
  12.     [SyncVar]
  13.     Quaternion syncRotaiton;
  14.     [SyncVar]
  15.     Vector2 syncVelocity;
  16.  
  17.     GameObject playerObject;
  18.     Transform playerTransform;
  19.  
  20.     void Start()
  21.     {
  22.         playerObject = gameObject;
  23.         playerTransform = GetComponent<Transform>();
  24.     }
  25.  
  26.     void Update()
  27.     {
  28.         LerpValues();
  29.     }
  30.  
  31.     void FixedUpdate()
  32.     {
  33.         if (isLocalPlayer)
  34.             CmdUpdatePosition(playerTransform.position, playerTransform.rotation, playerObject.GetComponent<Rigidbody2D>().velocity);
  35.     }
  36.  
  37.     void LerpValues()
  38.     {
  39.         if (!isLocalPlayer)
  40.         {
  41.             playerObject.GetComponent<Rigidbody2D>().velocity = syncVelocity;
  42.             playerTransform.rotation = syncRotaiton;
  43.  
  44.             playerTransform.position = Vector3.Lerp(playerTransform.position, syncPosition, Time.deltaTime * lerpAmount);
  45.         }
  46.     }
  47.  
  48.     [Command]
  49.     void CmdUpdatePosition(Vector2 position, Quaternion rotation, Vector2 velocity)
  50.     {
  51.         syncPosition = position;
  52.         syncRotaiton = rotation;
  53.         syncVelocity = velocity;
  54.     }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement