public void Update()
{
// Accumulate and update values used for measuring network performance
// and predicting next packet time.
TimeSinceLastNetworkFrame += Time.deltaTime;
// For the recent worst case, we want to quickly move towards bad times,
// and then more gently move back to the true average.
if (LastNetworkFrameTakeTime > RecentWorstNetworkTakeTime)
RecentWorstNetworkTakeTime = Mathf.Lerp(RecentWorstNetworkTakeTime, LastNetworkFrameTakeTime,
1f - Mathf.Pow(0.06f, Time.deltaTime));
else
RecentWorstNetworkTakeTime = Mathf.Lerp(RecentWorstNetworkTakeTime, LastNetworkFrameTakeTime,
1f - Mathf.Pow(0.60f, Time.deltaTime));
// This value will be used for actual network interpolation stuff
AverageTimeBetweenNetworkFrames = Mathf.Lerp(AverageTimeBetweenNetworkFrames, LastNetworkFrameTakeTime,
1f - Mathf.Pow(0.1f, Time.deltaTime));
// This value will be the one displayed to the users
SlowAverageTimeBetweenNetworkFrames = Mathf.Lerp(SlowAverageTimeBetweenNetworkFrames, RecentWorstNetworkTakeTime,
1f - Mathf.Pow(0.5f, Time.deltaTime));
if (Paused) return;
// Update enemies targeting us and related stuff
EnemiesTargetingUs.Update();
// Only interested in playing sound effects locally
if (networkView.isMine)
{
TimeSinceLastTargetedWarningPlayed += Time.deltaTime;
// Play sound effect if necessary
if (EnemiesTargetingUs.IsLockedByAnyEnemy &&
TimeSinceLastTargetedWarningPlayed >= TimeBetweenTargetedWarningNotification)
{
TimeSinceLastTargetedWarningPlayed = 0f;
if( GlobalSoundsScript.soundEnabled )
warningSound.Play();
}
}
if (networkView.isMine)
{
//TextBubbleVisible = ChatScript.Instance.showChat;
inputVelocity =
Input.GetAxis("Strafe") * transform.right +
Input.GetAxis("Thrust") * transform.forward;
if(inputVelocity.sqrMagnitude > 1)
inputVelocity.Normalize();
inputVelocity *= speed;
if (Input.GetButtonDown("Jump") && fallingVelocity.y <= 2 && !(sinceNotGrounded > 0 && jumpsSinceGrounded > 1 ) )
{
jumpsSinceGrounded++;
lastJumpInputTime = Time.time;
}
if (!Input.GetButton("Jump"))
{
activelyJumping = false;
if(fallingVelocity.y > 2)
fallingVelocity.y = 2;
}
if( lockMouse )
{
float invertMultiplier = invertMouse ? 1 : -1;
lookRotationEuler +=
MouseSensitivityScript.Sensitivity *
ZoomLookSensitivityMultiplier *
new Vector3(
Input.GetAxis("Vertical Look") * invertMultiplier,
Input.GetAxis("Horizontal Look"),
0);
}
lookRotationEuler.x = Mathf.Clamp(
lookRotationEuler.x, -lookAngleLimit, lookAngleLimit);
if( Input.GetMouseButtonUp( 0 ) && !Relay.Instance.ShowOptions )
{
lockMouse = true;
}
Cursor.lockState = ( lockMouse ? CursorLockMode.Locked : CursorLockMode.None );
Cursor.visible = !lockMouse;
/*else
{
Cursor.lockState = CursorLockMode.None;
Cursor.visible = true;
}*/
smoothYaw = lookRotationEuler.y;
smoothLookRotation = Quaternion.Euler(lookRotationEuler);
}
else
{
Vector3 desired = MathExts.LerpUnclamped(PreviousNetworkPosition, NewestNetworkPosition,
TimeSinceLastNetworkFrame / Mathf.Clamp(AverageTimeBetweenNetworkFrames, 0f, 5f));
// Might get some gross values on the first few lerps
if (!(float.IsNaN(desired.x) || float.IsNaN(desired.y) || float.IsNaN(desired.z)))
transform.position = desired;
var amt = (float)Math.Pow(0.0000000001, Time.deltaTime);
smoothLookRotation = Quaternion.Slerp(smoothLookRotation, Quaternion.Euler(lookRotationEuler), 1.0f - amt);
smoothYaw = smoothLookRotation.eulerAngles.y;
}
// set up text bubble visibility
if (!TextBubbleVisible)
{
var o = textBubble.GetComponent<Renderer>().material.color.a;
textBubble.GetComponent<Renderer>().material.color = new Color(1, 1, 1, Mathf.Clamp(o - Time.deltaTime * 10, 0, 0.875f));
if (o <= 0)
textBubble.GetComponent<Renderer>().enabled = false;
}
else
{
textBubble.GetComponent<Renderer>().enabled = true;
var o = textBubble.GetComponent<Renderer>().material.color.a;
textBubble.GetComponent<Renderer>().material.color = new Color(1, 1, 1, Mathf.Clamp(o + Time.deltaTime * 10, 0, 0.875f));
}
textBubble.transform.LookAt(Camera.main.transform);
textBubble.transform.localRotation = textBubble.transform.localRotation * Quaternion.Euler(90, 0, 0);
// sync up actual player and camera transforms
Vector3 euler = transform.rotation.eulerAngles;
euler.y = smoothYaw;
transform.rotation = Quaternion.Euler(euler);
cameraPivot.rotation = smoothLookRotation;
// dash animation
if (networkView.isMine)
{
Color color = dashEffectRenderer.material.GetColor("_TintColor");
Vector3 dashVelocity = new Vector3(fallingVelocity.x, activelyJumping ? 0 : Math.Max(fallingVelocity.y, 0), fallingVelocity.z);
if(dashVelocity.magnitude > 1/256.0)
{
color.a = dashVelocity.magnitude / dashForwardVelocity / 8;
dashEffectPivot.LookAt(transform.position + dashVelocity.normalized);
}
else
{
color.a = 0;
}
dashEffectRenderer.material.SetColor("_TintColor", color);
}
TimeSinceRocketJump += Time.deltaTime;
if(!controller.enabled) return;
if (Paused) return;
if (networkView.isMine)
UpdateMovement();
else
UpdateRemoteMovement();
// FIXME Hackity hack
if (networkView.isMine)
{
var indicator = Relay.Instance.MainCamera.GetComponent<WeaponIndicatorScript>();
indicator.HealthCapacity = HealthScript.maxHealth;
indicator.ShieldCapacity = HealthScript.maxShield;
indicator.HealthAvailable = HealthScript.Health;
indicator.ShieldAvailable = HealthScript.Shield;
}
}