public void Initialize(AttachedWeapon attached, Weapon weapon, Vector3 origin, GameObject target, Vector3 targetOffset) { if (target == null) { Destroy(gameObject); return; } attachedWeapon = attached; PrimaryWeapon = weapon; SecondaryWeapon = PrimaryWeapon.GetSecondaryWeapon(); Origin = origin; transform.position = origin; lifeTimer = PrimaryWeapon.ProjectileLife; Target = target; TargetOffset = targetOffset; if (PrimaryWeapon.isHoming) { isHoming = true; transform.rotation = Quaternion.LookRotation(target.transform.position - transform.position); additionalHomingTargets = PrimaryWeapon.HomingAdditionalTargets; if (PrimaryWeapon.PDTargetable) { transform.tag = "PDTarget"; Rigidbody rigidBody = gameObject.AddComponent <Rigidbody>(); rigidBody.isKinematic = true; Health = PrimaryWeapon.ProjectileHealth; GetShipManager().AddDPTargetableProjectile(this); } if (PrimaryWeapon.Jammable) { GetShipManager().AddJammableProjectile(this); } } else { if (target.transform.root.tag == "Ship" || target.transform.root.tag == "Fighter") { MobileSpaceUnit targetScript = target.transform.root.GetComponent <MobileSpaceUnit>(); RotateTowardsMovingTarget(targetScript); } else { transform.rotation = Quaternion.LookRotation(target.transform.position - transform.position); } } if (PrimaryWeapon.Spread > 0) { float spreadHalf = PrimaryWeapon.Spread / 2f; transform.Rotate(Random.Range(-spreadHalf, spreadHalf), Random.Range(-spreadHalf, spreadHalf), 0); } }
//Method to predict position of moving target public void RotateTowardsMovingTarget(MobileSpaceUnit target) { Vector3 targetDirection = target.transform.position - transform.position; float projectileSpeedSquared = PrimaryWeapon.ProjectileSpeed * attachedWeapon.baseWeapon.ProjectileSpeed; float tSpeed2 = target.GetVelocity().sqrMagnitude *GameManager.instance.GetGameSpeed(); float fDot1 = Vector3.Dot(targetDirection, target.GetVelocity()); float targetDistanceSquared = targetDirection.sqrMagnitude; float d = (fDot1 * fDot1) - targetDistanceSquared * (tSpeed2 - projectileSpeedSquared); if (d < 0.1f) // negative == no possible course because the interceptor isn't fast enough { transform.rotation = Quaternion.LookRotation(target.transform.position, Vector3.up); return; } float sqrt = Mathf.Sqrt(d); float S1 = (-fDot1 - sqrt) / targetDistanceSquared; float S2 = (-fDot1 + sqrt) / targetDistanceSquared; if (S1 < 0.0001f) { if (S2 < 0.0001f) { transform.rotation = Quaternion.LookRotation(target.transform.position, Vector3.up); return; } else { transform.rotation = Quaternion.LookRotation((S2)*targetDirection + target.GetVelocity(), Vector3.up); return; } } else if (S2 < 0.0001f) { transform.rotation = Quaternion.LookRotation((S1)*targetDirection + target.GetVelocity(), Vector3.up); return; } else if (S1 < S2) { transform.rotation = Quaternion.LookRotation((S2)*targetDirection + target.GetVelocity(), Vector3.up); return; } else { transform.rotation = Quaternion.LookRotation((S1)*targetDirection + target.GetVelocity(), Vector3.up); return; } }