void Update() { if (isShooting && !isNeed2Reload) { shotTime += Time.deltaTime; switch (weaponType) { case WeaponType.Projectile: if (shotTime >= 1f / fireRate) { shotTime = 0; LaunchShot(); } break; case WeaponType.LaserBeam: LaunchLaserBeam(); break; } } if (enableMagazine && magazineType == MagazineType.Time && !isShooting && magazineAutoReload) { magazineValue += reloadValue * Time.deltaTime; magazineValue = Mathf.Clamp(magazineValue, 0, magazineCapacity); } if (isAutonomous && radar) { if (!isShooting) { target = radar.GetTarget(cachedTransform, faction, radar.visible); if (target) { Shoot(target); } else { StopShoot(); } } else { if (target && target.gameObject.activeInHierarchy) { if (!radar.fov.IsVisble(cachedTransform, target)) { StopShoot(); } } else { StopShoot(); target = null; } } } }
void Update() { // Seek for detection if (state == TurretState.InRadarSeek && enableSeekTarget) { target = radar.GetTarget(cachedTransform, faction, true); // Target is found if (target != null) { targetOldPosition = target.transform.position; state = TurretState.InAcquisition; velocitySum = Vector3.zero; frameCount = 1; } // Reset target position else { float localHubYawAngle = hub.localEulerAngles.y > 180?hub.localEulerAngles.y - 360:hub.localEulerAngles.y; float localAxisAngle = axis.localEulerAngles.x > 180?axis.localEulerAngles.x - 360:axis.localEulerAngles.x; // Yaw if (hubRotation) { if (Mathf.Abs(localHubYawAngle) > speed * Time.deltaTime) { if (localHubYawAngle < 0) { hub.Rotate(Vector3.up * speed * Time.deltaTime, Space.Self); } else { hub.Rotate(Vector3.up * -speed * Time.deltaTime, Space.Self); } } } // Ptich if (axisRotation) { if (Mathf.Abs(localAxisAngle) > speed * Time.deltaTime) { if (localAxisAngle < 0) { axis.Rotate(Vector3.left * -speed * Time.deltaTime, Space.Self); } else { axis.Rotate(Vector3.left * speed * Time.deltaTime, Space.Self); } } } } } // Reach & track if (state != TurretState.InRadarSeek) { float distance = 0; // Target visible if (radar.fov.IsVisble(cachedTransform, target, out distance)) { float advance = 0; if (weaponSpeed > 0) { advance = (distance / weaponSpeed); } // Target + velocity in range Vector3 targetLookAt = target.transform.position + targetVelocity * advance; if (radar.fov.InFov(cachedTransform, targetLookAt, out distance)) { // In reach if (state == TurretState.InAcquisition) { Vector2 globalAngle = MathH.GetDeltaAngle(cachedTransform, targetLookAt, axis.localPosition); float localHubYawAngle = hub.localEulerAngles.y > 180?hub.localEulerAngles.y - 360:hub.localEulerAngles.y; float localAxisAngle = axis.localEulerAngles.x > 180?axis.localEulerAngles.x - 360:axis.localEulerAngles.x; // Yaw bool lockYaw = false; if (hubRotation) { if (Mathf.Abs(globalAngle.y - localHubYawAngle) > speed * Time.deltaTime) { if (localHubYawAngle < globalAngle.y) { hub.Rotate(Vector3.up * speed * Time.deltaTime, Space.Self); } else { hub.Rotate(Vector3.up * -speed * Time.deltaTime, Space.Self); } } else { lockYaw = true; } } else { lockYaw = true; } // Ptich bool lockPitch = false; if (axisRotation) { if (Mathf.Abs(globalAngle.x - localAxisAngle) > speed * Time.deltaTime) { if (localAxisAngle < globalAngle.x) { axis.Rotate(Vector3.left * -speed * Time.deltaTime, Space.Self); } else { axis.Rotate(Vector3.left * speed * Time.deltaTime, Space.Self); } } else { lockPitch = true; } } else { lockPitch = true; } if (lockPitch && lockYaw) { state = TurretState.InPursue; } } // In track else { if (hubRotation) { hub.LookAt(targetLookAt); hub.localRotation = Quaternion.Euler(new Vector3(0, hub.localRotation.eulerAngles.y, 0)); } if (axisRotation) { axis.LookAt(targetLookAt); axis.localRotation = Quaternion.Euler(new Vector3(axis.localRotation.eulerAngles.x, 0, 0)); } launcher.Shoot(target); } } else { launcher.StopShoot(); state = TurretState.InRadarSeek; //targetOldPosition = Vector3.zero; } } else { launcher.StopShoot(); state = TurretState.InRadarSeek; //targetOldPosition = Vector3.zero; } } }