/// <summary> /// Find the best target and turn the missile for a head-on collision. /// </summary> void Update() { float time = Time.time; if (mNextUpdate < time) { mNextUpdate = time + updateFrequency; // Find the most optimal target ahead of the missile if (currentTarget == null || !oneTarget) { currentTarget = HeatSource.Find(mTrans.position, mTrans.rotation * Vector3.forward, sensorRange, sensorAngle); } if (currentTarget != null) { // Calculate local space direction Vector3 dir = (currentTarget.transform.position - mTrans.position); float dist = dir.magnitude; dir *= 1.0f / dist; dir = Quaternion.Inverse(mTrans.rotation) * dir; // Make the missile turn slower if it's far away from the target, and faster when it's close float turnSensivitity = 0.5f + 2.5f * (1.0f - dist / sensorRange); // Calculate the turn amount based on the direction mTurn.x = Mathf.Clamp(dir.y * turnSensivitity, -1f, 1f); mTurn.y = Mathf.Clamp(dir.x * turnSensivitity, -1f, 1f); // Locked on target mTimeSinceTarget = 0f; } else { // No target lock -- keep track of how long it has been mTimeSinceTarget += updateFrequency + Time.deltaTime; } mControl.turningInput = mTurn; mControl.moveInput = Vector3.forward; } // If it has been too long if (mTimeSinceTarget > 2f) { Explosive exp = mControl.GetComponentInChildren <Explosive>(); if (exp != null) { exp.Explode(); } else { NetworkManager.RemoteDestroy(mControl.gameObject); } } }
/// <summary> /// Keep the reticle updated /// </summary> void Update() { if (!mIsPlayerControlled) { return; } // Find the target in front of the missile launcher that the missile would lock on if (mMissile != null && base.canFire) { mTarget = HeatSource.Find(mTrans.position, mTrans.rotation * Vector3.forward, mMissile.sensorRange, mMissile.sensorAngle); } else { mTarget = null; } // Update the player target if this is the player's missile launcher Player.target = (mTarget == null) ? null : mTarget.transform; }