Author Topic: Help Please(sync bugs...)  (Read 2279 times)

defender1213

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 4
    • View Profile
Help Please(sync bugs...)
« on: October 13, 2017, 04:16:12 PM »
I have a problem with sinc pos and rot,one player,who is in car can ride normal,but other player don't see it,because it stay(pos not changes)tis script.Help please to fix it!(I'am sorry if my English is bad...)
  1. using UnityEngine;
  2. using TNet;
  3.  
  4. /// <summary>
  5. /// Extended car that adds TNet-based multiplayer support.
  6. /// </summary>
  7. [RequireComponent(typeof(TNObject))]
  8. public class ExampleCar : ExampleCarNoNetworking
  9. {
  10.     /// <summary>
  11.     /// Maximum number of updates per second when synchronizing input axes.
  12.     /// The actual number of updates may be less if nothing is changing.
  13.     /// </summary>
  14.     private NetworkCar ncar;
  15.         [Range(1f, 20f)]
  16.         public float inputUpdates = 10f;
  17.  
  18.         /// <summary>
  19.         /// Maximum number of updates per second when synchronizing the rigidbody.
  20.         /// </summary>
  21.  
  22.         [Range(0.25f, 5f)]
  23.         public float rigidbodyUpdates = 1f;
  24.  
  25.         /// <summary>
  26.         /// We want to cache the network object (TNObject) we'll use for network communication.
  27.         /// If the script was derived from TNBehaviour, this wouldn't have been necessary.
  28.         /// </summary>
  29.  
  30.         [System.NonSerialized]
  31.         public TNObject tno;
  32.  
  33.         protected Vector2 mLastInput;
  34.         protected float mLastInputSend = 0f;
  35.         protected float mNextRB = 0f;
  36.     bool InCar;
  37.  
  38.         protected override void Awake ()
  39.         {
  40.                 base.Awake();
  41.                 tno = GetComponent<TNObject>();
  42.         ncar = GetComponent<NetworkCar>();
  43.  
  44.     }
  45.  
  46.         /// <summary>
  47.         /// Only the car's owner should be updating the movement axes, and the result should be sync'd with other players.
  48.         /// </summary>
  49.  
  50.         protected override void Update ()
  51.         {
  52.         // Only the player that actually owns this car should be controlling it
  53.         if (!ncar._allowToEnter || !tno.isMine || !ncar.InCar)
  54.             return;
  55.         base.Update();
  56.         float time = Time.time;
  57.         float delta = time - mLastInputSend;
  58.         float delay = 1f / inputUpdates;
  59.         // Update the input axes
  60.  
  61.  
  62.         // Don't send updates more than 20 times per second
  63.         if (delta > 0.05f)
  64.                 {
  65.                         // The closer we are to the desired send time, the smaller is the deviation required to send an update.
  66.                         float threshold = Mathf.Clamp01(delta - delay) * 0.5f;
  67.  
  68.             // Если отклонение является достаточно значительным, отправьте обновление другим игрокам
  69.             if (Tools.IsNotEqual(mLastInput.x, mInput.x, threshold) ||
  70.                                 Tools.IsNotEqual(mLastInput.y, mInput.y, threshold))
  71.                         {
  72.                                 mLastInputSend = time;
  73.                                 mLastInput = mInput;
  74.                                 tno.Send("SetAxis", Target.OthersSaved, mInput);
  75.                         }
  76.                 }
  77.  
  78.                 // Since the input is sent frequently, rigidbody only needs to be corrected every couple of seconds.
  79.                 // Faster-paced games will require more frequent updates.
  80.                 if (mNextRB < time)
  81.                 {
  82.                         mNextRB = time + 1f / rigidbodyUpdates;
  83.                         tno.Send("SetRB", Target.OthersSaved, mRb.position, mRb.rotation, mRb.velocity, mRb.angularVelocity);
  84.                 }
  85.         }
  86.  
  87.         /// <summary>
  88.         /// RFC for the input will be called several times per second.
  89.         /// </summary>
  90.  
  91.         [RFC]
  92.         protected void SetAxis (Vector2 v) { mInput = v; }
  93.  
  94.         /// <summary>
  95.         /// RFC for the rigidbody will be called once per second by default.
  96.         /// </summary>
  97.  
  98.         [RFC]
  99.         protected void SetRB (Vector3 pos, Quaternion rot, Vector3 vel, Vector3 angVel)
  100.         {
  101.                 mRb.position = pos;
  102.                 mRb.rotation = rot;
  103.                 mRb.velocity = vel;
  104.                 mRb.angularVelocity = angVel;
  105.         }
  106. }
  107.  
another script Network Car is used too.
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using TNet;
  5. public class NetworkCar : TNBehaviour
  6. {
  7.     public ExampleCar CC;
  8.     public Camera cam;
  9.     public bool InCar;
  10.     public bool _allowToEnter = false;
  11.     GameObject player;
  12.     protected void Update()
  13.     {
  14.         /*if (!InCar)
  15.         {
  16.             CC.enabled = false;
  17.         }*/
  18.         /*if (!tno.isMine)
  19.         {
  20.             CC.enabled = false;
  21.             cam.enabled = false;
  22.         }*/
  23.         if (!tno.isMine)
  24.             return;
  25.         if (!_allowToEnter)
  26.             return;
  27.         if (InCar)
  28.         {
  29.             if (Input.GetKeyDown(KeyCode.E) && _allowToEnter == false && InCar)
  30.             {
  31.                 Debug.Log("Exit");
  32.                 CarState(false);
  33.                 tno.Send("CarState",Target.OthersSaved,false);
  34.                 InCar = false;
  35.             }
  36.         }
  37.         if (InCar)
  38.             return;
  39.  
  40.         if (Input.GetKeyUp(KeyCode.E) && _allowToEnter == true && !InCar)
  41.         {
  42.             Debug.Log("Enter");
  43.             CarState(true);
  44.             tno.Send("CarState", Target.OthersSaved,true);
  45.             InCar = true;
  46.         }      
  47.     }
  48.     void OnTriggerEnter(Collider other)
  49.     {
  50.         if (other.tag == "Player")
  51.         {
  52.             _allowToEnter = true;
  53.             player = other.gameObject;
  54.         }
  55.     }
  56.     void OnTriggerExit(Collider other)
  57.     {
  58.         if (other.tag == "Player")
  59.         {
  60.             player = other.gameObject;
  61.             _allowToEnter = false;
  62.         }
  63.     }
  64.     [RFC(2)]void CarState(bool someone)
  65.     {
  66.         player.GetComponent<NetworkPlayer>().ChConState(!someone);
  67.         player.GetComponent<NetworkPlayer>().cam.gameObject.SetActive(!someone);
  68.         _allowToEnter = someone;
  69.         player.GetComponent<NetworkPlayer>().UpVisualState(!someone);
  70.  
  71.     }
  72.     }
  73.  
« Last Edit: October 13, 2017, 11:44:33 PM by defender1213 »