/// <summary> /// Initialization method, sets all the initial parameters /// </summary> /// <returns></returns> private bool Initialize() { if (!networkObject.IsServer) { if (networkObject.ownerNetId == 0) { _initialized = false; return(_initialized); } } _isLocalOwner = networkObject.MyPlayerId == networkObject.ownerNetId; if (_isLocalOwner || networkObject.IsServer) { networkObject.positionInterpolation.Enabled = false; if (_inputListener == null) { _inputListener = FindObjectsOfType <InputListener>() .FirstOrDefault(x => x.networkObject.Owner.NetworkId == networkObject.ownerNetId); if (_inputListener == null) { _initialized = false; return(_initialized); } } } _initialized = true; return(_initialized); }
private void PlayerUpdate(InputListener inputListener) { Move(inputListener); //PhysicsCollisions(); networkObject.position = _rigidBody.position; }
private void Move(InputListener inputListener) { bool useLerp = false; float time = Time.fixedDeltaTime; if (useLerp) { var newPosition = Vector2.Lerp(_rigidBody.position, inputListener.networkObject.mousePosition, speed * time); var oldPosition = _rigidBody.position; var diff = newPosition - oldPosition; if (diff.x > maxDiff) { newPosition.x = oldPosition.x + maxDiff; } else if (diff.x < -maxDiff) { newPosition.x = oldPosition.x - maxDiff; } if (diff.y > maxDiff) { newPosition.y = oldPosition.y + maxDiff; } else if (diff.y < -maxDiff) { newPosition.y = oldPosition.y - maxDiff; } _rigidBody.position = newPosition; } else { Vector2 position = inputListener.networkObject.mousePosition - _rigidBody.position; float signX = Math.Sign(position.x); float signY = Math.Sign(position.y); Vector2 velocity = new Vector2(signX, signY) * speed * time; _rigidBody.velocity = velocity; _rigidBody.position += _rigidBody.velocity; } }
// Return true if find input listener or return false private bool FindInputListener() { if (_inputListener == null) { if (networkObject.IsServer) { InputListener[] listeners = FindObjectsOfType <InputListener>(); foreach (InputListener listener in listeners) { if (listener.networkObject.Owner.NetworkId == OwnerNetworkId) { _inputListener = listener; return(true); } } } else { InputListener[] listeners = FindObjectsOfType <InputListener>(); foreach (InputListener listener in listeners) { if (listener.networkObject.IsOwner) { _inputListener = listener; return(true); } } } return(false); } else { return(true); } }
void FixedUpdate() { // Check if this client is the local owner _isLocalOwner = networkObject.MyPlayerId == networkObject.ownerNetId; #region Setup // Initial setup - only do this once if ((_isLocalOwner || networkObject.IsServer) && !_setup) { // Interpolation on the predicted client and server does weird things // Only interpolate on non-owner clients networkObject.positionInterpolation.Enabled = false; _setup = true; } // Get the input listener if it doesn't exist and this isn't a remote client if (_inputListener == null) { _inputListener = FindObjectsOfType <InputListener>().FirstOrDefault(x => x.networkObject.Owner.NetworkId == networkObject.ownerNetId); } #endregion #region Netcode Logic // Server Authority - snap the position on all clients to the server's position if (!networkObject.IsServer) { _rigidBody.position = networkObject.position; } // Client owner Reconciliation & Prediction if (_isLocalOwner) { if (_inputListener != null) { // Reconciliation - only do this if the server update is current or new if (networkObject.frame != 0 && _lastNetworkFrame <= networkObject.frame) { _lastNetworkFrame = networkObject.frame; Reconcile(); } // Prediction if (_inputListener.FramesToPlay.Count > 0) { InputFrame input = _inputListener.FramesToPlay[0]; _lastLocalFrame = input.frameNumber; PlayerUpdate(input); _inputListener.FramesToPlay.RemoveAt(0); } } } // Server Processing else if (networkObject.IsServer) { // Reset the current input - we don't want to re-use it if there no inputs in the queue _currentInput = null; if (_inputListener != null) { // Process all available inputs each frame while (_inputListener.FramesToPlay.Count > 0) { _currentInput = _inputListener.FramesToPlay[0]; _lastLocalFrame = _currentInput.frameNumber; _inputListener.FramesToPlay.RemoveAt(0); // Try-catch is a good idea to handle weird serialization/deserialization errors try { PlayerUpdate(_currentInput); } catch (Exception e) { Debug.LogError(e + " (Serverside input processing - Player.cs line 104)"); } } } } #endregion }