Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ]/// ---------------------------------------------
- /// Ultimate Character Controller
- /// Copyright (c) Opsive. All Rights Reserved.
- /// https://www.opsive.com
- /// ---------------------------------------------
- using UnityEngine;
- using Opsive.UltimateCharacterController.Character.Abilities;
- using Opsive.UltimateCharacterController.Character.Abilities.Items;
- using Opsive.UltimateCharacterController.Game;
- using Opsive.UltimateCharacterController.Events;
- using Opsive.UltimateCharacterController.Input;
- using Opsive.UltimateCharacterController.StateSystem;
- using Opsive.UltimateCharacterController.Utility;
- using System.Collections.Generic;
- using TNet;
- namespace Opsive.UltimateCharacterController.Character
- {
- /// <summary>
- /// The UltimateCharacterLocomotionHandler manages player input and the UltimateCharacterLocomotion.
- /// </summary>
- [RequireComponent(typeof(TNObject))]
- public class NetworkedUltimateCharacterLocomotionHandler : UltimateCharacterLocomotionHandler
- {
- [Range(1f, 20f)]
- public float inputUpdates = 10f;
- [Range(0.25f, 5f)]
- public float rigidbodyUpdates = 1f;
- [System.NonSerialized]
- public TNObject tno;
- protected Vector2 mLastInput;
- protected float mLastInputSend = 0f;
- protected float mNextRB = 0f;
- private System.Collections.Generic.List<ActiveInputEvent> m_ActiveInputList;
- private Rigidbody mRb;
- private Vector2 mInput = new Vector2(0f, 0f);
- /// <summary>
- /// Initialize the default values.
- /// </summary>
- protected override void Awake()
- {
- base.Awake();
- tno = GetComponent<TNObject>();
- mRb = GetComponent<Rigidbody>();
- m_CharacterLocomotion = GetComponent<UltimateCharacterLocomotion>();
- }
- /// <summary>
- /// Updates the input.
- /// </summary>
- protected override void Update()
- {
- // Update the input axes
- base.Update();
- if (!tno.isMine)
- {
- Debug.Log("Not mine.");
- return;
- }
- if (tno.creatorPlayerID != TNManager.player.id)
- {
- Debug.Log("Not mine.");
- return;
- }
- // Objects get marked as destroyed while being transferred from one channel to another
- if (tno.hasBeenDestroyed) return;
- float time = Time.time;
- float delta = time - mLastInputSend;
- float delay = 1f / inputUpdates;
- mInput.x = m_HorizontalMovement;
- mInput.y = m_ForwardMovement;
- // Don't send updates more than 20 times per second
- if (delta > 0.05f)
- {
- // The closer we are to the desired send time, the smaller is the deviation required to send an update.
- float threshold = Mathf.Clamp01(delta - delay) * 0.5f;
- // If the deviation is significant enough, send the update to other players
- if (Tools.IsNotEqual(mLastInput.x, mInput.x, threshold) ||
- Tools.IsNotEqual(mLastInput.y, mInput.y, threshold))
- {
- mLastInputSend = time;
- mLastInput = mInput;
- tno.Send("SetAxis", Target.OthersSaved, mInput);
- }
- }
- // Since the input is sent frequently, rigidbody only needs to be corrected every couple of seconds.
- // Faster-paced games will require more frequent updates.
- if (mNextRB < time)
- {
- mNextRB = time + 1f / rigidbodyUpdates;
- tno.Send("SetRB", Target.OthersSaved, mRb.position, mRb.rotation, mRb.velocity, mRb.angularVelocity);
- }
- }
- [RFC]
- protected void SetAxis(Vector2 v) { mInput = v; m_HorizontalMovement = mInput.x; m_ForwardMovement = mInput.y; }
- /// <summary>
- /// RFC for the rigidbody will be called once per second by default.
- /// </summary>
- [RFC]
- protected void SetRB(Vector3 pos, Quaternion rot, Vector3 vel, Vector3 angVel)
- {
- mRb.position = pos;
- mRb.rotation = rot;
- mRb.velocity = vel;
- mRb.angularVelocity = angVel;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement