[TNet.RCC(10)]
void Update()
{
float time = Time.time;
// We're ready to fire -- fire the cannon
if (mFireTime != 0f && mFireTime <= time)
{
// Recharge time varies from 80% to 120% of the intended duration just to add variety
mRechargeTime = time + rechargeTime * Mathf.Lerp(0.8f, 1.2f, Random.value);
mFireTime = 0f;
// Create the cannon ball
if (cannonballPrefab != null)
{
// Instantiate the prefab
GameObject go = TNManager.Instantiate(cannonballPrefab, mTrans.position, mTrans.rotation) as GameObject;
if (mColliders != null)
{
Collider col = go.GetComponent<Collider>();
if (col != null)
{
foreach (Collider c in mColliders)
{
Physics.IgnoreCollision(c, col);
}
}
}
// It's usually a good idea to know who fired the cannon ball
Cannonball cb = go.GetComponent<Cannonball>();
if (cb != null) cb.owner = mStats.gameObject;
// Rigidbody is generally expected to be present
Rigidbody rb = go.GetComponent<Rigidbody>();
if (rb != null)
{
Vector2 deviation = Vector2.zero;
// Deviate the aim a little
if (maxAimDeviationAngle > 0f)
{
deviation
= new Vector2
(Random
.Range(-1f, 1f
), Random
.Range(-1f, 1f
)); deviation.Normalize();
deviation *= maxAimDeviationAngle;
}
// Calculate the initial velocity
Quaternion firingDir = Quaternion.LookRotation(mFiringDir);
Vector3 vel = (firingDir * Quaternion.Euler(-mFiringPitch + deviation.x, deviation.y, 0f)) *
Vector3.forward * initialVelocity;
// NOTE: For physics-accurate results we should append the ship's velocity as well,
// but this makes the auto-aiming logic fail unless the ship isn't moving.
rb.velocity = vel;
TNManager.CreateEx (10,false,go);
}
else
{
Debug.LogWarning("The cannon ball is missing its rigidbody");
}
}
}
}