/*void Update()
* {
* gun.LookAt(playerCamera.GetTargetPosition());
*
* if (playerScript.Paused)
* bulletsLeft = BurstCount;
* }*/
// TODO unused, looks like it should go below in the homing firing
//WeaponIndicatorScript.PlayerData GetFirstTarget()
//{
// var aimedAt = targets.Where(x => x.SinceInCrosshair >= AimingTime );
// return aimedAt.OrderBy( x => Guid.NewGuid() ).First();
//}
public void Update()
{
var actualTargetPosition = playerCamera.GetTargetPosition();
firingDirection = (actualTargetPosition - gun.transform.position).normalized;
var gunRotationAngles = Quaternion.FromToRotation(Vector3.forward, firingDirection).eulerAngles;
var desiredGunRotation = Quaternion.Euler(gunRotationAngles.x, gunRotationAngles.y, 0);
gun.transform.rotation = Quaternion.Slerp(gun.transform.rotation, desiredGunRotation,
1.0f - Mathf.Pow(GunRotationSmoothingSpeed, -GunRotationSmoothingSpeed * Time.deltaTime));
var doReload = false;
// Update local position of barrel from recoil
foreach (var impulse in GunRecoilThrotter.Update())
{
GunRecoilSpring.AddImpulse(impulse);
}
GunRecoilSpring.Update();
Vector3 barrelLocalPosition = BarrelTransform.localPosition;
barrelLocalPosition.z = GunRecoilSpring.CurrentValue;
BarrelTransform.localPosition = barrelLocalPosition;
// Reload spring
foreach (var impulse in ReloaderDelayer.Update())
{
ReloaderSpring.AddImpulse(impulse);
}
ReloaderSpring.Update();
Vector3 reloadLocalPosition = ReloaderTransform.localPosition;
reloadLocalPosition.z = ReloaderSpring.CurrentValue;
ReloaderTransform.localPosition = reloadLocalPosition;
if (playerScript.Paused)
{
bulletsLeft = BurstCount;
}
if (networkView.isMine && playerScript.lockMouse && !playerScript.Paused)
{
cooldownLeft = Mathf.Max(0, cooldownLeft - Time.deltaTime);
heat = Mathf.Clamp01(heat - (Time.deltaTime * HeatCooldownScale));
weaponIndicator.CooldownStep = 1 - Math.Min(Math.Max(cooldownLeft - ShotCooldown, 0) / ReloadTime, 1);
if (cooldownLeft <= Mathf.Epsilon)
{
// Shotgun
if (Input.GetButton("Alternate Fire"))
{
OnShotFired();
OnShotgunFired();
// Rail shot
if (bulletsLeft == BurstCount && playerCamera.IsZoomedIn)
{
DoRailShot();
bulletsLeft = 0;
cooldownLeft += ReloadTime * 2.7f;
var recoilImpulse = -playerCamera.LookingDirection * 2.35f;
recoilImpulse *= playerScript.controller.isGrounded ? 25 : 87.5f;
recoilImpulse.y *= playerScript.controller.isGrounded ? 0.1f : 0.375f;
playerScript.AddRecoil(recoilImpulse);
}
// Homing/shotgun
else
{
// find homing target(s)
var aimedAt = targets.Where(x => x.SinceInCrosshair >= AimingTime).ToArray();
var bulletsShot = bulletsLeft;
var first = true;
int homingFired = 0;
while (bulletsLeft > 0)
{
if (!aimedAt.Any())
{
DoHomingShot(ShotgunSpread, null, 0, first);
}
else
{
var pd = aimedAt.OrderBy(x => Guid.NewGuid()).First();
DoHomingShot(ShotgunSpread, pd.Script, Mathf.Clamp01(pd.SinceInCrosshair / AimingTime) * ShotgunHomingSpeed, first);
homingFired++;
}
cooldownLeft += ShotCooldown;
first = false;
}
cooldownLeft += ReloadTime;
float lockOnReduction = (float)homingFired / (float)BurstCount;
float amountOfLockToRemove = WeaponIndicatorScript.PlayerData.LockStrengthLimitMultiplier *
lockOnReduction;
for (int i = 0; i < targets.Count; i++)
{
targets[i].LockStrength -= amountOfLockToRemove;
targets[i].ClampStrength();
}
var recoilImpulse = -gun.forward * ((float)bulletsShot / BurstCount);
recoilImpulse *= playerScript.controller.isGrounded ? 25 : 87.5f;
recoilImpulse.y *= playerScript.controller.isGrounded ? 0.1f : 0.375f;
playerScript.AddRecoil(recoilImpulse);
float cosmeticAmount = (float)bulletsShot / (float)BurstCount;
playerCamera.AddGunShotImpulse(Mathf.Lerp(0.0f, 2.0f, cosmeticAmount));
AddGunImpulse(cosmeticAmount);
}
//cannonChargeCountdown = CannonChargeTime;
}
// Burst
else if (Input.GetButton("Fire")) // burst fire
{
//OnShotFired();
DoShot(BurstSpread);
cooldownLeft += CurrentShotCooldown;
}
if (Input.GetButton("Reload") || bulletsLeft <= 0)
{
doReload = true;
}
}
if (Input.GetButtonUp("Fire"))
{
doReload = true;
}
if (doReload && bulletsLeft != BurstCount)
{
cooldownLeft += ReloadTime * (float)(BurstCount - bulletsLeft) / (float)BurstCount;
bulletsLeft = BurstCount;
if (GlobalSoundsScript.soundEnabled)
{
reloadSound.Play();
}
AddReloadImpulse();
}
TestScreenSpaceLockTargets();
CheckTargets();
// Update weapon indicator bullets count. Kind of hacky because
// bullet availability is not tracked like you might expect it to
// be.
weaponIndicator.BulletCapacity = BurstCount;
if (bulletsLeft == BurstCount && cooldownLeft >= Mathf.Epsilon)
{
weaponIndicator.BulletsAvailable = 0;
}
else if (cooldownLeft > Mathf.Epsilon && weaponIndicator.BulletsAvailable < bulletsLeft)
{
// Do nothing, probably reloading
}
else
{
weaponIndicator.BulletsAvailable = bulletsLeft;
}
}
}