public bool AttemptFire() { bool wasFired = false; if (Time.time > lastFireTime + specs.FireDelay) { var targetPoint = GunMaths.ComputeGunLead( Target.Position, Target.Velocity, turret.position, Vector3.zero, specs.MuzzleVelocity); var rotationToTarget = Quaternion.LookRotation(targetPoint - turret.position); var laser = Object.Instantiate(laserPrefab, turret.position, rotationToTarget); laser.Damage = specs.Damage; laser.Fire(ownShip, specs.MuzzleVelocity); lastFireTime = Time.time; audio.transform.position = turret.position; audio.Play(); wasFired = true; } return(wasFired); }
private void StrafeTarget() { if (AttackTarget == null) { return; } if (strafeState == StrafeState.Strafing) { Throttle = .8f; bool shouldExtend = AttackTarget.DistanceFromAllColliders(ownTarget.Position) < 200f; if (shouldExtend) { extendPosition = ownTarget.Up * 1000f + AttackTarget.Position; strafeState = StrafeState.Extending; IsFiring = false; } else { var attackPoint = AttackTarget.GetLocalToWorldPoint(strafeTargetOffset); attackPoint = GunMaths.ComputeGunLead( attackPoint, AttackTarget.Velocity, ownTarget.Position, Vector3.zero, ownShip.Specs.Weapons.MuzzleVelocity); TurnTowards(AttackTarget.Transform.TransformPoint(strafeTargetOffset)); // Fire when within parameters. var angleToAttack = Vector3.Angle(ownTarget.Forward, attackPoint - ownTarget.Position); var distanceToAttack = Vector3.Distance(attackPoint, ownTarget.Position); IsFiring = angleToAttack < 5f && distanceToAttack < 500f; } } else { Throttle = 1f; IsFiring = false; var distanceToExtend = Vector3.Distance(extendPosition, ownTarget.Position); if (distanceToExtend < 100f) { strafeState = StrafeState.Strafing; strafeTargetOffset = AttackTarget.GetRandomPointOnTarget(); } else { TurnTowards(extendPosition); } } }
private void DogfightTarget() { if (AttackTarget == null) { return; } // Evasive maneuvers! var distance = Vector3.Distance(AttackTarget.Position, ownTarget.Position); if (distance < 100f) { IsFiring = false; TurnTowards(AttackTarget.Position + preferredAvoidOffset); Throttle = 0.4f; } else { // Get lead on target. var targetPoint = GunMaths.ComputeGunLead( AttackTarget.Position, AttackTarget.Velocity, ownTarget.Position, Vector3.zero, ownShip.Specs.Weapons.MuzzleVelocity); TurnTowards(targetPoint, Mathf.PerlinNoise(seed, Time.time / 10f) * MaxTurnPower); var angleToTarget = Vector3.Angle( ownShip.transform.forward, AttackTarget.Position - ownTarget.Position); IsFiring = angleToTarget < 5f && IsFireAllowed && distance < 300f; if (Vector3.Angle(AttackTarget.Forward, ownTarget.Forward) < 90) { // Adjust throttle based on distance so faster ships don't overtake slower ones. Throttle = Maths.Remap(50f, 250f, .33f, .8f, distance); } else { Throttle = 0.85f; } } }