Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.63 KB | None | 0 0
  1. ]/// ---------------------------------------------
  2.  
  3. /// Ultimate Character Controller
  4.  
  5. /// Copyright (c) Opsive. All Rights Reserved.
  6.  
  7. /// https://www.opsive.com
  8.  
  9. /// ---------------------------------------------
  10.  
  11.  
  12.  
  13. using UnityEngine;
  14.  
  15. using Opsive.UltimateCharacterController.Character.Abilities;
  16.  
  17. using Opsive.UltimateCharacterController.Character.Abilities.Items;
  18.  
  19. using Opsive.UltimateCharacterController.Game;
  20.  
  21. using Opsive.UltimateCharacterController.Events;
  22.  
  23. using Opsive.UltimateCharacterController.Input;
  24.  
  25. using Opsive.UltimateCharacterController.StateSystem;
  26.  
  27. using Opsive.UltimateCharacterController.Utility;
  28.  
  29. using System.Collections.Generic;
  30.  
  31. using TNet;
  32.  
  33.  
  34.  
  35. namespace Opsive.UltimateCharacterController.Character
  36.  
  37. {
  38.  
  39.     /// <summary>
  40.  
  41.     /// The UltimateCharacterLocomotionHandler manages player input and the UltimateCharacterLocomotion.
  42.  
  43.     /// </summary>
  44.  
  45.     [RequireComponent(typeof(TNObject))]
  46.  
  47.     public class NetworkedUltimateCharacterLocomotionHandler : UltimateCharacterLocomotionHandler
  48.  
  49.     {
  50.  
  51.         [Range(1f, 20f)]
  52.  
  53.         public float inputUpdates = 10f;
  54.  
  55.  
  56.  
  57.         [Range(0.25f, 5f)]
  58.  
  59.         public float rigidbodyUpdates = 1f;
  60.  
  61.  
  62.  
  63.         [System.NonSerialized]
  64.  
  65.         public TNObject tno;
  66.  
  67.  
  68.  
  69.         protected Vector2 mLastInput;
  70.  
  71.  
  72.  
  73.         protected float mLastInputSend = 0f;
  74.  
  75.         protected float mNextRB = 0f;
  76.  
  77.        
  78.  
  79.         private System.Collections.Generic.List<ActiveInputEvent> m_ActiveInputList;
  80.  
  81.         private Rigidbody mRb;
  82.  
  83.        
  84.  
  85.         private Vector2 mInput = new Vector2(0f, 0f);
  86.  
  87.         /// <summary>
  88.  
  89.         /// Initialize the default values.
  90.  
  91.         /// </summary>
  92.  
  93.         protected override void Awake()
  94.  
  95.         {
  96.  
  97.             base.Awake();
  98.  
  99.             tno = GetComponent<TNObject>();
  100.  
  101.             mRb = GetComponent<Rigidbody>();
  102.  
  103.             m_CharacterLocomotion = GetComponent<UltimateCharacterLocomotion>();
  104.  
  105.         }
  106.  
  107.  
  108.  
  109.         /// <summary>
  110.  
  111.         /// Updates the input.
  112.  
  113.         /// </summary>
  114.  
  115.         protected override void Update()
  116.  
  117.         {
  118.  
  119.  
  120.  
  121.             // Update the input axes
  122.  
  123.             base.Update();
  124.  
  125.             if (!tno.isMine)
  126.  
  127.             {
  128.  
  129.                 Debug.Log("Not mine.");
  130.  
  131.                 return;
  132.  
  133.             }
  134.  
  135.             if (tno.creatorPlayerID != TNManager.player.id)
  136.  
  137.             {
  138.  
  139.                 Debug.Log("Not mine.");
  140.  
  141.                 return;
  142.  
  143.             }
  144.  
  145.             // Objects get marked as destroyed while being transferred from one channel to another
  146.  
  147.             if (tno.hasBeenDestroyed) return;
  148.  
  149.  
  150.  
  151.             float time = Time.time;
  152.  
  153.             float delta = time - mLastInputSend;
  154.  
  155.             float delay = 1f / inputUpdates;
  156.  
  157.             mInput.x = m_HorizontalMovement;
  158.  
  159.             mInput.y = m_ForwardMovement;
  160.  
  161.             // Don't send updates more than 20 times per second
  162.  
  163.             if (delta > 0.05f)
  164.  
  165.             {
  166.  
  167.                 // The closer we are to the desired send time, the smaller is the deviation required to send an update.
  168.  
  169.                 float threshold = Mathf.Clamp01(delta - delay) * 0.5f;
  170.  
  171.  
  172.  
  173.                 // If the deviation is significant enough, send the update to other players
  174.  
  175.                 if (Tools.IsNotEqual(mLastInput.x, mInput.x, threshold) ||
  176.  
  177.                     Tools.IsNotEqual(mLastInput.y, mInput.y, threshold))
  178.  
  179.                 {
  180.  
  181.                     mLastInputSend = time;
  182.  
  183.                     mLastInput = mInput;
  184.  
  185.                     tno.Send("SetAxis", Target.OthersSaved, mInput);
  186.  
  187.                 }
  188.  
  189.             }
  190.  
  191.  
  192.  
  193.             // Since the input is sent frequently, rigidbody only needs to be corrected every couple of seconds.
  194.  
  195.             // Faster-paced games will require more frequent updates.
  196.  
  197.             if (mNextRB < time)
  198.  
  199.             {
  200.  
  201.                 mNextRB = time + 1f / rigidbodyUpdates;
  202.  
  203.                 tno.Send("SetRB", Target.OthersSaved, mRb.position, mRb.rotation, mRb.velocity, mRb.angularVelocity);
  204.  
  205.             }
  206.  
  207.         }
  208.  
  209.  
  210.  
  211.         [RFC]
  212.  
  213.         protected void SetAxis(Vector2 v) { mInput = v; m_HorizontalMovement = mInput.x; m_ForwardMovement = mInput.y; }
  214.  
  215.  
  216.  
  217.         /// <summary>
  218.  
  219.         /// RFC for the rigidbody will be called once per second by default.
  220.  
  221.         /// </summary>
  222.  
  223.  
  224.  
  225.         [RFC]
  226.  
  227.         protected void SetRB(Vector3 pos, Quaternion rot, Vector3 vel, Vector3 angVel)
  228.  
  229.         {
  230.  
  231.             mRb.position = pos;
  232.  
  233.             mRb.rotation = rot;
  234.  
  235.             mRb.velocity = vel;
  236.  
  237.             mRb.angularVelocity = angVel;
  238.  
  239.         }
  240.  
  241.     }
  242.  
  243. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement