Example #1
0
    public IEnumerator WarmUpGun()
    {
        /* This function provides a delay before allowing the turret to shoot,
         * to avoid having an overpowered turret that fires at the player
         * instantly once they come around a corner.
         */
        yield return(new WaitForSeconds(shootDelay));

        if (state == TURRET_BEHAVIOUR_STATE.PreparingFire)
        {
            state = TURRET_BEHAVIOUR_STATE.Firing;
        }
        yield return(null);
    }
Example #2
0
    private void StopShooting()
    {
        if (state == TURRET_BEHAVIOUR_STATE.PreparingFire || state == TURRET_BEHAVIOUR_STATE.Firing)
        {
            Debug.Log(transform.name + ": Stopping Turret (line of sight lost), now searching randomly");
            state = TURRET_BEHAVIOUR_STATE.Searching;
        }
        switch (searchMode)
        {
        case TURRET_SEARCH_TYPE.PointToPoint:
            SearchPtP();
            break;

        case TURRET_SEARCH_TYPE.Random:
            SearchRandomly();
            break;

        default:
            SearchRandomly();
            break;
        }
    }
Example #3
0
    void Update()
    {
        // Does the turret have a target?
        if (target)
        {
            // Is the target within viewing distance?
            float distToTarget = Vector3.Distance(target.transform.position, rotationPiece.transform.position);
            if (distToTarget <= trackingRange)
            {
                // Only allow line of sight if the player is in front of the turret
                float angleDegreesToPlayer = Vector3.Angle(rotationPiece.forward, (target.transform.position - rotationPiece.transform.position).normalized);
                if (angleDegreesToPlayer < visionAngle)
                {
                    // Raycast to check if the turret has line of sight to the target
                    Vector3      angleToTarget = target.transform.position - rotationPiece.transform.position;
                    RaycastHit   hit           = new RaycastHit();
                    RaycastHit[] hits          = Physics.RaycastAll(new Ray(rotationPiece.position, angleToTarget), Vector3.Distance(target.transform.position, rotationPiece.transform.position) + 0.01f);
                    Debug.DrawRay(rotationPiece.position, angleToTarget, Color.red);

                    // Find first collision that shouldn't be ignored (prevents losing LOS if a bullet, etc gets in the way)
                    foreach (RaycastHit h in hits)
                    {
                        bool hitVisible = true;

                        // Ignore the turret itself including all child objects
                        if (!(h.transform.IsChildOf(transform)))
                        {
                            // Check if raycast hit an object that should be ignored
                            foreach (string s in seeThroughTags)
                            {
                                if (h.collider.tag == s)
                                {
                                    hitVisible = false;
                                    break;
                                }
                            }
                        }
                        else
                        {
                            hitVisible = false;
                        }

                        if (hitVisible)
                        {
                            hit = h;
                            break;
                        }
                    }

                    if (hit.collider)
                    {
                        if (hit.collider.gameObject == target || hit.collider.transform.IsChildOf(target.transform))
                        {
                            // Turret has line of sight to player, rotate to look at the player
                            RotateToFacePoint(target.transform.position);

                            // Is the target close to directly in front of the turret?
                            if (angleDegreesToPlayer <= 5.0f)
                            {
                                if (!((state == TURRET_BEHAVIOUR_STATE.PreparingFire) || (state == TURRET_BEHAVIOUR_STATE.Firing)))
                                {
                                    // Warm up the turret
                                    state = TURRET_BEHAVIOUR_STATE.PreparingFire;
                                    Debug.Log("Warming up turret");
                                    StartCoroutine(WarmUpGun());
                                }
                            }

                            TryShoot();
                        }
                        else
                        {
                            StopShooting();
                        }
                    }
                    else
                    {
                        StopShooting();
                    }
                }
                else
                {
                    StopShooting();
                }
            }
            else
            {
                StopShooting();
            }
        }
    }