// Checks which direction to go in every once in a while void Update() { /* TODO We can tweak the update rate at which we send whisp updates * updates based on the player's position * sending really few updates for players that are far away * and relatively many updates to the players that are close by */ if (_clock.CurrentTime > _lastUpdateTime + _navigationInterval) { _direction = Vector3.Slerp(_direction, Random.insideUnitSphere, _randomness).normalized; RaycastHit hitInfo; bool hit = Physics.Raycast(transform.position, Vector3.down, out hitInfo, _raycastLength); if (hit) { float normalizedAltitude = Mathf.Clamp01((transform.position.y - hitInfo.point.y) / _maxAltitude); _direction = Vector3.Lerp(_direction, Vector3.down, Mathf.Pow(normalizedAltitude, _altitudeLimitPower)); _direction = Vector3.Lerp(_direction, Vector3.up, Mathf.Pow(1f - normalizedAltitude, _altitudeLimitPower)); } _lastUpdateTime = _clock.CurrentTime; var updateDirectionMessage = _directionMessages.Create(); updateDirectionMessage.Content.Position = transform.position; updateDirectionMessage.Content.Direction = _direction; _messageSender.Send(updateDirectionMessage, ObjectRoles.NonAuthoritive); } _transform.position = WhispSimulation.Simulate(_transform.position, _direction, _configuration, _clock.DeltaTime); }
void Update() { // Calculate where the whisp should be based on the received direction // Calculate where it is now //Smoothly transition between the two Vector3 newPos = WhispSimulation.Simulate(_transform.position, _direction, _configuration, _clock.DeltaTime); Vector3 correctedPos = WhispSimulation.Simulate(_serverPosition, _direction, _configuration, (float)(_clock.CurrentTime - _serverTimestamp)); _transform.position = Vector3.Lerp(newPos, correctedPos, _syncSpeed * _clock.DeltaTime); }